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




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