pat 1030

 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

 

 

题意:想从A地去B地旅行,要求找到从A到B的最短路径,如果有多条最短路径,选取其中花费最小的一条.
思路:根据所给的信息构建图,这里我是用邻接矩阵存储的。然后使用Dijkstra算法求解单源最短路径,这里的源点就是出发点A,注意到
最短路径有多条,因此需要用一个vector类型的变长数组记录某个顶点的前一个顶点(
这个顶点可能有多个,要加到vector后面)
。得到A到B的最短路径之后采用dfs算法,递归求出最短路径中花费最少的路径。


  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<vector>
  4 #define inf 999999
  5 using namespace std;
  6 bool collected[505]={false};
  7 vector<int> pre[505];
  8 int dist[505];
  9 int n,m,s,d;
 10 typedef struct{
 11     int dis;
 12     int cost;
 13 }node;
 14 node G[505][505];
 15 int minCost=inf;
 16 vector<int> path,temppath;
 17 void dfs(int v){
 18     temppath.push_back(v);
 19     if(v==s){
 20         int sum=0;
 21         for(int i=0;i<temppath.size()-1;i++){
 22             int a1=temppath[i];
 23             int a2=temppath[i+1];
 24             sum+=G[a1][a2].cost;
 25         }
 26         if(sum<minCost){
 27             path=temppath;
 28             minCost=sum;
 29         }
 30         temppath.pop_back();
 31         return;
 32     }
 33     for(int i=0;i<pre[v].size();i++){
 34         dfs(pre[v][i]);
 35     }
 36     temppath.pop_back();
 37 }
 38 int findMin(){
 39     int minV=-1;
 40     int minLength=inf;
 41     for(int i=0;i<n;i++){
 42         if(collected[i]==false&&dist[i]<minLength){
 43             minV=i;
 44             minLength=dist[i];
 45         }
 46     }
 47     return minV;
 48 }
 49 void dijkstra(){
 50     for(int i=0;i<n;i++){
 51         if(G[s][i].dis<inf){
 52             dist[i]=G[s][i].dis;
 53             pre[i].push_back(s);
 54         }
 55     }
 56     dist[s]=0;
 57     collected[s]=true;
 58     while(1){
 59         int v=findMin();
 60         if(v==-1)
 61             break;
 62         collected[v]=true;
 63         for(int i=0;i<n;i++){
 64             if(collected[i]==false&&G[v][i].dis<inf){
 65                 if(G[v][i].dis+dist[v]<dist[i]){
 66                     dist[i]=G[v][i].dis+dist[v];
 67                     pre[i].clear();
 68                     pre[i].push_back(v);
 69                 }
 70                 else if(G[v][i].dis+dist[v]==dist[i]){
 71                     pre[i].push_back(v);
 72                 }
 73             }
 74         }
 75     }
 76 
 77 }
 78 int main(){
 79     for(int i=0;i<505;i++){
 80         dist[i]=inf;
 81         for(int j=0;j<505;j++){
 82             G[i][j].cost=G[i][j].dis=inf;
 83             
 84         }
 85     } 
 86 
 87     int a,b,dis,cost;
 88     scanf("%d%d%d%d",&n,&m,&s,&d);
 89     for(int i=0;i<m;i++){
 90         scanf("%d%d%d%d",&a,&b,&dis,&cost);
 91         G[a][b].dis=G[b][a].dis=dis;
 92         G[a][b].cost=G[b][a].cost=cost;
 93     }
 94     dijkstra();
 95     dfs(d);
 96     int length=0;
 97     for(int i=path.size()-1;i>=0;i--){
 98         printf("%d ",path[i]);    
 99         if(i!=0)
100         length+=G[path[i]][path[i-1]].dis;
101     } 
102     printf("%d ",length);
103     printf("%d",minCost);
104     return 0;
105 } 

 

 
posted @ 2020-06-16 16:05  9761滴  阅读(181)  评论(0)    收藏  举报