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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值