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

时间:2022-07-26
本文章向大家介绍PAT (Advanced Level) Practice 1030 Travel Plan (30分),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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.

Sample Input:

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:

0 2 3 3 40

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

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

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