PAT - 甲级 - 1030. Travel Plan (30) (Dijkstra+DFS)
2017-10-31 18:45
507 查看
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
Sample Output
题目有2个要求:
1.求出最短路
2.求出最短路中需要的花费最低的路
解决方法:
1.Dijkstra算法求出最短路,并且记录路径
2.遍历最短路的所有路径,找出花费最少的路
3.输出最短+花费最少的路
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
题目有2个要求:
1.求出最短路
2.求出最短路中需要的花费最低的路
解决方法:
1.Dijkstra算法求出最短路,并且记录路径
2.遍历最短路的所有路径,找出花费最少的路
3.输出最短+花费最少的路
#include<iostream> #include<cstdio> #include<vector> #define INF 999999999 using namespace std; int a, b, c, d; int N, M, S, D; int e[505][505]; int cost[505][505]; int dis[505]; int vis[505]; int mincost = INF; vector<int> path; vector<int> temp; vector<int> pre[505]; void dfs(int v){ if(v == S){ temp.push_back(S); int sum = 0; for(int i = 1; i < temp.size(); i++){ int i1 = temp[i-1]; int i2 = temp[i]; sum += cost[i1][i2]; } if(sum < mincost){ mincost = sum; path = temp; } temp.pop_back(); return ; } temp.push_back(v); for(int i = 0; i < pre[v].size(); i++){ dfs(pre[v][i]); } temp.pop_back(); } int main(){ freopen("input.txt", "r", stdin); while(scanf("%d%d%d%d",&N, &M, &S, &D) != EOF){ // init fill(e[0], e[0] + 505 * 505, INF); fill(dis, dis + 505, INF); fill(vis, vis + 505, 0); // input for(int i = 0; i < M; i++){ scanf("%d%d%d%d",&a, &b, &c, &d); e[a][b] = e[b][a] = c; cost[a][b] = cost[b][a] = d; } // Dijkstra dis[S] = 0; for(int i = 0; i < N; i++){ int x = -1, minl = INF; for(int j = 0; j < N; j++){ if(!vis[j] && dis[j] < minl){ x = j; minl = dis[j]; } } if(x == -1) break; vis[x] = 1; for(int j = 0; j < N; j++){ if(!vis[j] && e[x][j] != INF){ if(dis[x] + e[x][j] < dis[j]){ dis[j] = dis[x] + e[x][j]; pre[j].clear(); pre[j].push_back(x); } else if(dis[x] + e[x][j] == dis[j]){ pre[j].push_back(x); } } } } //dfs dfs(D); // output for(int i = path.size()-1; i >=0; i--){ if(i==path.size()-1) printf("%d",path[i]); else printf(" %d",path[i]); } printf(" %d %d\n",dis[D],mincost); } return 0; }
相关文章推荐
- 1030. Travel Plan (30)-PAT甲级真题(Dijkstra + DFS,输出路径,边权)
- PAT (Advanced Level)1030. Travel Plan (30) dfs
- 1030. Travel Plan (30)-PAT甲级真题
- PAT 1030. Travel Plan (30)(Dijkstra,最短路径的同时计算最小奥cost)
- PAT - 甲级 - 1087. All Roads Lead to Rome (30)(Dijkstra+DFS+路径)
- PAT 甲级 1030. Travel Plan (30)
- PAT 甲级 1030. Travel Plan (30)
- 1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS
- PAT甲级 1030. Travel Plan (30)
- PAT甲级1030. Travel Plan(30)
- PAT甲级1030. Travel Plan (30)
- pat甲级1030. Travel Plan (30)
- 1018. Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS)
- 1030. Travel Plan (30) PAT 甲级
- PAT (Advanced Level) 1030. Travel Plan (30) Dijkstra最短路径
- PAT - 甲级 - 1018. Public Bike Management (30)(Dijkstra+DFS)
- 【PAT甲级】1030. Travel Plan (30)
- 1111. Online Map (30)-PAT甲级真题(Dijkstra + DFS)
- PAT 1030. Travel Plan (30)
- PAT - 甲级 - 1004. Counting Leaves (30)(DFS/BFS,二叉树的遍历)