前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >PAT (Advanced Level) Practice 1030 Travel Plan (30分)

PAT (Advanced Level) Practice 1030 Travel Plan (30分)

作者头像
glm233
发布2020-09-28 11:02:38
发布2020-09-28 11:02:38
26700
代码可运行
举报
运行总次数:0
代码可运行

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:

代码语言:javascript
代码运行次数:0
复制
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.

Sample Input:

代码语言:javascript
代码运行次数:0
复制
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

代码语言:javascript
代码运行次数:0
复制
0 2 3 3 40

多源最短路板子题,但是带了一点PAT的一贯作风,要求在最短路中再找一条花费最小的路径,因为之前找的最短路是路径最短且不唯一,现在要你找出这些最短路中花费最小的路径

那还能怎么办呢,上dijkstra板子,但要做一点修改,在松弛步骤中,如果找到路径相同的,比较其花费,但是如果找到路径小的直接将花费改成该条路径的,因为路径长度被考虑优先级最高,算是30分中比较中规中矩的一题吧~

代码语言:javascript
代码运行次数:0
复制
#include<bits/stdc++.h>
#define inf 2147483647
#define ll long long
#define rg register ll
using namespace std;
ll n,m,s,d;
ll vis[505],dis[505],fee[505],val[505][505],cost[505][505],path[505];
inline void work()
{
    while(!vis[d])
    {
        ll v=-1,minn=inf;
        for(rg i=0;i<n;i++)
        {
            if(!vis[i]&&dis[i]<minn)
            {
                v=i,minn=dis[i];
            }
        } 
        if(!(~v))return ;
       
        vis[v]=1;
        for(rg i=0;i<n;i++)
        {
            if(!vis[i]&&dis[v]+val[v][i]<dis[i])
            {
                dis[i]=dis[v]+val[v][i];
                fee[i]=fee[v]+cost[v][i];
                path[i]=v;
            }
            if(!vis[i]&&dis[v]+val[v][i]==dis[i]&&fee[v]+cost[v][i]<fee[i])
            {
                fee[i]=fee[v]+cost[v][i];
                path[i]=v;
            }
        }
    }
}
inline void display(ll x)
{
    if(x==s)cout<<s;
    else
    {
        display(path[x]);
        cout<<" "<<x;
    }
}
int main()
{
    cin>>n>>m>>s>>d;
    fill(dis,dis+505,inf);
    fill(fee,fee+505,inf);
    fill(val[0],val[0]+505*505,inf);
    fill(cost[0],cost[0]+505*505,inf);
    for(rg i=0;i<m;i++)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        val[a][b]=val[b][a]=c,cost[a][b]=cost[b][a]=d;
    }
    dis[s]=0,fee[s]=0;
    work();
    display(d);
    cout<<" "<<dis[d]<<" "<<fee[d]<<endl;
    //while(1)getchar();
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/12/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档