PAT甲級1030. Travel Plan(30)

1030. 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
這個題目的意思是求最短路,如果最短路有多條,每條邊還有個cost,求cost累加和最短的那一條路,求該路路徑長度和cost和。

最短路就直接用dijkstra,稍微改一改,就可以變成這題的解法。


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include <stack>


using namespace std;


int grond[502][502];    //路長
int val[502][502];      //價值
int n,m,st,en;
int vis[501];       //走過的路
int d[501];         //最短路長
int co[501];        //最少價值
int pre[501];       //路徑
void djs(){             //dikjstra
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    for(int i = 0;i<n;i++)
    {
        d[i]=grond[st][i];
        co[i]=val[st][i];
    }
    d[st]=0;
    co[st]=0;
    vis[st]=1;
    while(1)
    {
        int k = -1;
        for(int j = 0 ;j<n;j++)
        {
            if(!vis[j] && (k==-1||d[k]>d[j]))//找到當前沒用過的最接近原點的點
            {
                k=j;
            }
        }
        if(k==-1)
            break;
        vis[k]=1;
        for(int j=0;j<n;j++)//對選中的點更新其他所有點
            if(!vis[j] && d[j]>grond[k][j]+d[k]){
                d[j]=grond[k][j]+d[k];
                co[j]=val[k][j]+co[k];
                pre[j]=k;//記錄前一個數字
            }
            else if(!vis[j] && d[j]==grond[k][j]+d[k] && co[j]>val[k][j]+co[k]){
                co[j]=val[k][j]+co[k];
                pre[j]=k;
            }
    }
}


int main()
{
    cin>>n>>m>>st>>en;
    memset(grond,999999,sizeof(grond));
    memset(val,999999,sizeof(val));
    for(int i = 0 ;i<m;i++){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        grond[a][b]=grond[b][a]=c;
        val[a][b]=val[b][a]=d;
    }
    djs();
    stack<int> ans;//對於路徑倒序輸出
    int tem = en;
    while(tem!=-1)
    {
        ans.push(tem);
        tem = pre[tem];
    }
    cout<<st<<" ";
    while(!ans.empty())
    {
        cout<<ans.top()<<" ";
        ans.pop();
    }
    cout<<d[en]<<" "<<co[en]<<endl;
	return 0;
}




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