1030. Travel Plan (30)
来源:互联网 发布:辐射4捏脸数据存档 编辑:程序博客网 时间:2024/06/18 16:53
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<stdio.h>#define inf 999999999int n,m,start,end;int mark[510];int dis[510];int path[510];int sumcost[510];int adj[510][510];int cost[510][510];void init(){int i,j;for(i=0;i<n;i++){dis[i]=inf;for(j=0;j<n;j++){adj[i][j]=inf;}adj[i][i]=0;}}void dijkstra(int s){mark[s]=1;dis[s]=0;int i,newp=s;while(1){for(i=0;i<n;i++){if(mark[i]==0&&adj[newp][i]<inf){if(dis[i]>dis[newp]+adj[newp][i]){dis[i]=dis[newp]+adj[newp][i];sumcost[i]=sumcost[newp]+cost[newp][i];path[i]=newp;}else if(dis[i]==dis[newp]+adj[newp][i]){if(sumcost[i]>sumcost[newp]+cost[newp][i]){sumcost[i]=sumcost[newp]+cost[newp][i];path[i]=newp;}}}}int min=inf;for(i=0;i<n;i++){if(mark[i]==0&&dis[i]<inf){if(dis[i]<min){min=dis[i];newp=i;}}}mark[newp]=1;if(min==inf||newp==end){break;}}}void print(int s){if(s==start){printf("%d ",s);return ;}print(path[s]);printf("%d ",s);}int main(){int i,c1,c2,distance,money;scanf("%d %d %d %d",&n,&m,&start,&end);init();for(i=0;i<m;i++){scanf("%d %d %d %d",&c1,&c2,&distance,&money);adj[c1][c2]=adj[c2][c1]=distance;cost[c1][c2]=cost[c2][c1]=money;}dijkstra(start);print(end);printf("%d %d",dis[end],sumcost[end]);}
- 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)
- AngularJS Service、Factory、Provider 的理解与使用区别
- 多线程的几种实现方式(简易版)--Callable接口介绍
- Android 原生HttpURLConnection网络请求工具类(get post)
- 阿里面试经历(二)
- Redux
- 1030. Travel Plan (30)
- JavaSE学习总结-坦克大战
- 新生多校第6场 LLM找对象(DP+离散化)
- 包装类
- MySQL高阶之事务、存储结构、索引、触发器
- Bigtable: A Distributed Storage System for Structured Data : part10 Related Work
- 动画的枚举UIViewAnimationOptions
- LeetCode 643. Maximum Average Subarray I
- 使用gstreamer搭建简易的播放器