1030. Travel Plan (30)

来源:互联网 发布:辐射4捏脸数据存档 编辑:程序博客网 时间:2024/06/18 16:53

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 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 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20
Sample 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]);} 


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信帐号解封后漂流瓶不能用怎么办 微信怎么在电脑上登不上去怎么办 玩旧版60级魔兽经常花屏怎么办? 我的世界手机版物品栏不见了怎么办 苹果手机掉进水里出现花屏该怎么办 球球大作战还没进去停止运行怎么办 ps3 e3硬破芯片坏了怎么办 电话打开后页面上没有东西怎么办 WPS在电脑安装后卸载不了怎么办 ps总要以管理员的身份打开怎么办 3d关的慢保存慢怎么办 无法与服务器建立可靠的连接怎么办 被抵押的房子开发商不解押怎么办 手机系统语言是英文没有中文怎么办 w7主机网插口灯不亮了没网怎么办 电脑用了5年变得很卡了怎么办 苹果6s系统占12g怎么办 百度网盘下载的压缩包打不开怎么办 三星手机微信安装包解析错误怎么办 下身流出来的东西有异味很重怎么办 鞋脚底总有一股酸臭的味道怎么办 腋窝总是有股酸臭的味道应该怎么办 房间里总是有灰尘的味道怎么办 香水喷多了衣服味道太呛怎么办 香水喷在衣服上变黄了怎么办 84喷在白衣服上变红怎么办 夏季高温多肉山地玫瑰砍头了怎么办 海水缸爆藻有些石头不出绿藻怎么办 不小心把b原来的壁纸删了怎么办 注册简历时没有固定电话必填怎么办 申诉找回微信账号密码验证码怎么办 激活淘宝时显示注册账号过多怎么办 科学上网之后电脑上不了网怎么办 阴阳师地域鬼王定位在非洲怎么办 网吧忘记下机有小孩玩我号怎么办 等了三年老公还是不跟小三断怎么办 小三生孩子老公为责任断不了怎么办 没离婚去医院生孩子刷身份证怎么办 小孩早晨起床鼻子痒有鼻涕怎么办 孩子鼻梁上方和眼眶磕肿了怎么办 宝宝开空调冻的流鼻涕打喷嚏怎么办