1030. Travel Plan (30)
来源:互联网 发布:剑侠情缘网络单机版 编辑:程序博客网 时间:2024/06/15 03:57
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
算法解析:考察点明确,就是单源最短路问题,用dijkstra算法就能解决,只需要做一点改动。
#include <stdio.h>#include <stdlib.h>#define INF 9999999//gdist存储每个城市之间的距离,gcost存储每个城市之间的路费,dist存储从起点到各点的最短距离,//cost类似,path存储路劲,visited表示该城市是否被使用。int gdist[500][500], gcost[500][500], dist[500], cost[500], path[500], visited[500];//初始化void init(int n, int v){ int i; for(i = 0; i < n; i++) { visited[i] = 0; dist[i] = gdist[v][i]; cost[i] = gcost[v][i]; if(dist[i] < INF) //path[i]=v,表示i的上一个顶点是v。 path[i] = v; else path[i] = -1; //-1表示i是根节点。 }}void solve(int n, int v, int w){ int i, j; visited[v] = 1; dist[v] = cost[v] = 0; for(j = 0; j < n; j++) { int maxDist = INF, u = v; for(i = 0; i < n; i++) //选出离起点最近的点。 { if(!visited[i] && dist[i] < maxDist) { u = i; maxDist = dist[i]; } } visited[u] = 1; for(i = 0; i < n; i++) { if(!visited[i] && gdist[u][i] < INF) { int newDist = dist[u] + gdist[u][i]; int newCost = cost[u] + gcost[u][i]; if(newDist < dist[i]) { dist[i] = newDist; cost[i] = newCost; path[i] = u; } else if(newDist == dist[i] && newCost < cost[i]) //距离相等就比较路费 { cost[i] = newCost; path[i] = u; } } } } int stack[500], top = -1, tmp = w; //创建堆栈,反向输出。 while(tmp > -1) { stack[++top] = tmp; tmp = path[tmp]; } for(i = top; i >= 0; i--) printf("%d ", stack[i]); printf("%d %d", dist[w], cost[w]);}int main(){ int n, m, v, w, i, j, v1, v2, t1, t2; scanf("%d %d %d %d", &n, &m, &v, &w); for(i = 0; i < n; i++) for(j = 0; j < n; j++) gdist[i][j] = gcost[i][j] = INF; for(i = 0; i < m; i++) { scanf("%d %d %d %d", &v1, &v2, &t1, &t2); gdist[v1][v2] = gdist[v2][v1] = t1; gcost[v1][v2] = gcost[v2][v1] = t2; } init(n, v); solve(n, v, w); return 0;}
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- JavaSE阶段1_类
- python 进程 Ctrl+C退出
- <li></li>标签的属性/样式
- 局域网映射的几种方式
- 2017年一级消防工程师高薪就业
- 1030. Travel Plan (30)
- JavaSE阶段1_抽象类和接口
- redis 缓存失效原理
- poj2251 Dungeon Master(B)
- 51单片机自动循迹小车
- 湖南省第十一届大学生计算机程序设计竞赛—大还是小?
- 数据库索引的优缺点
- 白盒测试和黑盒测试_项目中的测试
- JavaSE阶段1_面向对象封装