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;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/84189172