1. 程式人生 > >PAT (Advanced Level) Practice 1030 Travel Plan (30 分)單源最短路變式

PAT (Advanced Level) Practice 1030 Travel Plan (30 分)單源最短路變式

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

題意
給出一個無向帶權值的圖,求最短路徑的最小花費。

思路:

迪傑斯特拉的變式,在縮點的時候加上當d[v]==d[u]+edge[v][u]的情況,並對花費進行縮邊。

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
const int maxn=505;
const int INF=0x3f3f3f3f;
int n,m,s,dd;
int d[maxn];
int head[maxn];
int pre[maxn];
int spend[maxn];
struct edge
{
    int to;
    int next;
    int w;
    int sp;
};
struct node
{
    int v;
    int dis;
    int sp;
    bool operator < (const node x) const
    {
        if(dis!=x.dis)
            return  dis > x.dis;
        return sp>x.sp;
    }
};
edge e[maxn*2];
void addedge(int u,int v,int w,int id,int sp)
{
    e[id].to=v;
    e[id].w=w;
    e[id].sp=sp;
    e[id].next=head[u];
    head[u]=id;
}
void djst(int s)
{
    int vis[maxn];
    priority_queue<node>q;
    for (int i=0;i<n;i++)
    {
        vis[i]=0;
        spend[i]=d[i]=INF;
    }
    d[s]=0; spend[s]=0;
    node t; t.v=s; t.dis=0; t.sp=0;
    q.push(t);
    while (!q.empty())
    {
         t=q.top();q.pop();
         int u=t.v; int dis=t.dis; int sp=t.sp;
         if(vis[u]) continue;
         vis[u]=1;
         for (int i=head[u];i!=-1;i=e[i].next)
         {
              int v=e[i].to;
              int w=e[i].w;
              int sp=e[i].sp;
              if(!vis[v])
              {
                  if(d[v]>d[u]+w)
                  {
                      d[v]=d[u]+w;
                      pre[v]=u;
                      spend[v]=sp+spend[u];
                      t.v=v; t.dis=d[v]; t.sp=spend[v];
                      q.push(t);
                  }
                  if(d[v]==d[u]+w)
                  {
                      if(spend[v]>spend[u]+sp)
                      {
                          pre[v]=u;
                          spend[v]=spend[u]+sp;
                          t.v=v; t.dis=d[v]; t.sp=spend[v];
                          q.push(t);
                      }
                  }

              }
         }
    }
}
int main()
{
    memset (head,-1,sizeof(head));
    memset (pre,-1,sizeof(pre));
    scanf("%d%d%d%d",&n,&m,&s,&dd);
    for (int i=0,id=0;i<m;i++)
    {
        int x,y,sp,len;
        scanf("%d%d%d%d",&x,&y,&len,&sp);
        addedge(x,y,len,id++,sp);
        addedge(y,x,len,id++,sp);
    }
    djst(s);
    stack<int>S;
    int temp=dd;
    while (temp!=-1)
    {
        S.push(temp);
        temp=pre[temp];
    }
    while (!S.empty())
    {
        printf("%d",S.top());
        S.pop();
        printf(" ");
    }
    printf("%d %d\n",d[dd],spend[dd]);
    return 0;
}