1030 Travel Plan (30 分)

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 (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; Sand 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

最短路的板子题算是。
 1 #include <bits/stdc++.h>
 2 #define N 600
 3 #define inf 0x3f3f3f3f
 4 using namespace std;
 5 int n, m, s, d;
 6 struct Node{
 7     int to, val, cost;
 8     friend bool operator<(const Node &a, const Node &b){
 9         if(a.val == b.val)
10             return a.cost > b.cost;
11         return a.val > b.val;
12     }
13 };
14 stack<int> st;
15 vector<Node> v[N];
16 int value[N],vis[N];
17 int path[N];
18 int fa[N];
19 int min_val = inf, min_cost = inf;
20 priority_queue<Node> q;
21 void dij(int x){
22     Node node;
23     node.to = x, node.val = 0, node.cost = 0;
24     q.push(node);
25     while(!q.empty()){
26         node = q.top();
27         q.pop();
28         if(vis[node.to])
29             continue;
30         vis[node.to] = 1;
31         value[node.to] = min(value[node.to], node.val);
32         for(int i = 0 ; i < v[node.to].size(); i++){
33             Node ans = v[node.to][i];
34             ans.val += value[node.to];
35             ans.cost += node.cost;
36             if(ans.val <= value[ans.to]){
37                 value[ans.to] = ans.val;
38                 path[ans.to] = node.to;
39             }
40             if(ans.to == d){
41                 if(ans.val < min_val){
42                     min_val = ans.val;
43                     min_cost = ans.cost;
44                     for(int i = 0 ; i <= n; i++){
45                         fa[i] = path[i];
46                     }
47                 }else if(ans.val == min_val){
48                     if(ans.cost < min_cost){
49                         min_cost = ans.cost;
50                         for(int i = 0 ; i <= n; i++){
51                             fa[i] = path[i];
52                         }
53                     }
54                 }
55             }
56             q.push(ans);
57         }
58     }
59
60 }
61
62 int main(){
63     cin >> n >> m >> s >> d;
64     int w,x,y,z;
65     for(int i = 0; i < m; i++){
66         cin >> x >> y >> z >> w;
67         v[x].push_back({y,z,w});
68         v[y].push_back({x,z,w});
69     }
70     memset(value, inf, sizeof(value));
71     dij(s);
72     while(d != s){
73         st.push(d);
74         d = fa[d];
75     }
76     cout << s;
77     while(!st.empty()){
78         cout <<" "<<st.top();
79         st.pop();
80     }
81     cout <<" " <<min_val<<" "<<min_cost<<endl;
82     return 0;
83 }

原文地址:https://www.cnblogs.com/zllwxm123/p/11075075.html

时间: 2024-10-10 07:57:27

1030 Travel Plan (30 分)的相关文章

1030 Travel Plan (30分)

最短路径问题.一定要注意将visited[u]是否等于0加入判断!很关键,还是没有理解透,所以才忘了加.(第42行和第45行) 其实图相关的题通过率蛮高的...套路题,坑少. 1 #include <iostream> 2 #include<cstdio> 3 #include<vector> 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 struct Edge{ 7 int next,dis,cos; 8 }; 9

PAT 1030. Travel Plan (30)

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 cit

1030 Travel Plan (30)(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.

1030. Travel Plan (30)

dfs使用vector保存最短路径 时间限制 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 tr

PAT (Advanced Level) 1030. Travel Plan (30)

先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; const int

PAT:1030. Travel Plan (30) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int N,M,S,D; //城市数,高速公路数,起点,终点 bool visit[MAXV]; //标记在迪杰斯特拉中是否遍历过 int GD[MAXV][MAXV]; //存储距离图 int GC[MAXV][MAXV];

A1030 Travel Plan (30分)

一.技术总结 这一题是关于图的遍历,但是涉及Djikstra算法,在求最短路径的同时,还要把路径记录下来:同时增加了边权,也就会每个城市之间的花费: 这里采用Djikstra算法+DFS遍历的方法 第一步使用Djikstra算法求出最短路径,使用vector类型pre数组进行存储,每个结点里面保存的是他们的前驱结点. 然后再使用DFS遍历pre,然后会有得出遍历树. 要注意下标是0~n-1,还是1~n. 细节问题参考代码 二.参考代码 #include<bits/stdc++.h> using

pat1030. 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

1030 Travel Plan (30 分)

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 ci