PAT A1030. Travel Plan (30)

  1. Travel Plan (30)

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
16000 B
判題程序
Standard
作者
CHEN, Yue
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

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=505;
const int inf=0x3fffffff;
int N,M,S,D;
int G[maxn][maxn];
int C[maxn][maxn];
int dis[maxn];
int cost[maxn];
bool vis[maxn];
int pre[maxn];
void Dijkstra(int s){
    fill(vis,vis+maxn,false);
    fill(dis,dis+maxn,inf);
    fill(cost,cost+maxn,inf);
    dis[s]=0;
    cost[s]=0;
    for(int i=0;i<N;i++){
        int min=inf,u=-1;
        for(int i=0;i<N;i++){
            if(vis[i]==false&&dis[i]<min){
                u=i;
                min=dis[i];
            }
        }
        if(u==-1)return;
        vis[u]=true;
        for(int v=0;v<N;v++){
            if(vis[v]==false&&G[u][v]!=inf){
                if(dis[v]>dis[u]+G[u][v]){
                    dis[v]=dis[u]+G[u][v];
                    cost[v]=cost[u]+C[u][v];
                    pre[v]=u;
                }else if(dis[v]==dis[u]+G[u][v]){
                    if(cost[v]>cost[u]+C[u][v]){
                        cost[v]=cost[u]+C[u][v];
                        pre[v]=u;
                    }
                }
            }
        }
    }
}

void DFS(int v){
    if(v==S){
        printf("%d ",v);
        return;
    }
    DFS(pre[v]);
    printf("%d ",v);
}
int main(){
    fill(G[0],G[0]+maxn*maxn,inf);
    fill(C[0],C[0]+maxn*maxn,inf);
    scanf("%d %d %d %d",&N,&M,&S,&D);
    for(int i=0;i<M;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        scanf("%d %d",&G[a][b],&C[a][b]);
        G[b][a]=G[a][b];
        C[b][a]=C[a][b];
    }
    Dijkstra(S);
    DFS(D);
    printf("%d %d",dis[D],cost[D]);
    return 0;
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章