码迷,mamicode.com
首页 > 其他好文 > 详细

1030 Travel Plan (30)

时间:2018-06-13 21:45:47      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:bre   const   case   rom   res   arc   end   cost   must   

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
参考地址:https://www.liuchuo.net/archives/2369
 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 vector<int> path, temp, pre[501];
 5 vector<bool> vis(501, false);
 6 const int inf=99999999;
 7 int s, d, minncost=inf;
 8 int cost[501][501], e[501][501], dis[501];
 9 void dfs(int v){
10   temp.push_back(v);
11   if(v==s){
12     int tempcost=0;
13     for(int j=temp.size()-1; j>0; j--){
14       int id=temp[j], nextid=temp[j-1];
15       tempcost += cost[id][nextid];
16     }
17     if(minncost>tempcost){
18       minncost=tempcost;
19       path=temp;
20     }
21     temp.pop_back();
22     return;
23   }
24   for(int i=0; i<pre[v].size(); i++) dfs(pre[v][i]);
25   temp.pop_back();
26 }
27 int main(){
28   int i, j, n, m;
29   cin>>n>>m>>s>>d;
30   fill(e[0], e[0]+501*501, inf);
31   fill(dis, dis+501, inf);
32   for(i=0; i<m; i++){
33     int a, b;
34     scanf("%d %d", &a, &b);
35     scanf("%d %d", &e[a][b], &cost[a][b]);
36     cost[b][a]=cost[a][b]; e[b][a]=e[a][b];
37   }
38   pre[s].push_back(s);
39   dis[s]=0;
40   for(i=0; i<n; i++){
41     int u=-1, minn=inf;
42     for(j=0; j<n; j++){
43       if(!vis[j] && dis[j]<minn){
44         minn=dis[j];
45         u=j;
46       }
47     }
48     if(u==-1) break;
49     vis[u]=true;
50     for(int v=0; v<n; v++){
51       if(!vis[v] && e[u][v]!=inf){
52         if(dis[v]>dis[u]+e[u][v]){
53           dis[v] = dis[u]+e[u][v];
54           pre[v].clear();
55           pre[v].push_back(u);
56         }else if(dis[v] == dis[u]+e[u][v]){
57           pre[v].push_back(u);
58         }
59       }
60     }
61   }
62   dfs(d);
63   for(i=path.size()-1; i>=0; i--) printf("%d ", path[i]);
64   printf("%d %d", dis[d], minncost);
65   return 0;
66 }

 

1030 Travel Plan (30)

标签:bre   const   case   rom   res   arc   end   cost   must   

原文地址:https://www.cnblogs.com/mr-stn/p/9180174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!