PAT (Advanced Level) Practise 1030 Travel Plan (30)
来源:互联网 发布:linux部署环境 编辑:程序博客网 时间:2024/06/02 00:12
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 Input4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20Sample Output
0 2 3 3 40
#include<cstdio>#include<string>#include<cstring>#include<vector>#include<iostream>#include<queue>#include<bitset>#include<algorithm>using namespace std;typedef long long LL;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 5e2 + 10;int n, m, s, t, map[maxn][maxn], cost[maxn][maxn], x, y, z, c;int dis[maxn], v[maxn];void dfs(int x){if (x == t) return;for (int i = 0; i < n; i++){if (map[x][i]){if (dis[i]>dis[x] + map[x][i]){dis[i] = dis[x] + map[x][i];v[i] = v[x] + cost[x][i];dfs(i);}else if (dis[i] == dis[x] + map[x][i] && v[i] > v[x] + cost[x][i]){v[i] = v[x] + cost[x][i];dfs(i);}}}}bool Dfs(int x){if (x == s) { printf("%d ", s); return true; }for (int i = 0; i < n; i++){if (map[x][i] && dis[x] == dis[i] + map[x][i] && v[x] == v[i] + cost[x][i]){if (Dfs(i)) { printf("%d ", x); return true; }}}return false;}int main(){scanf("%d%d%d%d", &n, &m, &s, &t);while (m--){scanf("%d%d%d%d", &x, &y, &z, &c);if (!map[x][y] || map[x][y] > z){map[x][y] = map[y][x] = z;cost[x][y] = cost[y][x] = c;}else if (map[x][y] == z) cost[x][y] = cost[y][x] = min(z, cost[x][y]); }for (int i = 0; i < n; i++) dis[i] = v[i] = INF;dis[s] = v[s] = 0;dfs(s);Dfs(t);printf("%d %d\n", dis[t], v[t]);return 0;}
- PAT (Advanced Level) Practise 1030 Travel Plan (30)
- PAT (Advanced Level) Practise 1030 Travel Plan (30)
- Pat(Advanced Level)Practice--1030(Travel Plan)
- 【PAT Advanced Level】1030. Travel Plan (30)
- 【PAT】【Advanced Level】1030. Travel Plan (30)
- 1030. Travel Plan (30)【最短路+路径记忆】——PAT (Advanced Level) Practise
- PAT Advanced Level 1030. Travel Plan (30)(Java and C++)
- PAT (Advanced) 1030. Travel Plan (30)
- PAT (Advanced Level) 1030. Travel Plan (30) Dijkstra最短路径
- [PAT] (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- Delphi基于TIdhttp方法发送cookie实例
- 若遇到TFTP、FTP服务器端口冲突
- ios中srcroot和project_dir的区别
- PAT-A1089. Insert or Merge (25)
- PL/SQL之六过程与函数
- PAT (Advanced Level) Practise 1030 Travel Plan (30)
- 自是人生长恨水长东
- Android学习笔记----Handler
- HDU 5636 Shortest Path
- PL/SQL编程之七-包
- DisplayMetrics
- 欢迎使用CSDN-markdown编辑器
- Android四大组件详解
- web service design log