hdu1874 畅通工程续(求最短路径)

网友投稿 234 2022-12-01

hdu1874 畅通工程续(求最短路径)

畅通工程续

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 35206    Accepted Submission(s): 12877

Problem Description

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input

本题目包含多组数据,请处理到文件结束。 每组数据第一行包含两个正整数N和M(0

Output

对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input

3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2

Sample Output

2 -1

Author

linle

Source

Recommend

lcy   |   We have carefully selected several similar problems for you:   ​​2112​​​  ​​​1217​​​  ​​​1142​​​  ​​​1690​​​  ​​​1548​​

​​Statistic​​ |

​​Submit​​ |

​​Discuss​​ |

​​Note​​

dijkstr算法,做过好多这样的题了,也解释了那么多。。。这个就不详细说了吧..........

#include #include #include using namespace std;struct node//实现优先队列的结构体{ int pos,cost; friend bool operator<(node x,node y) { return x.cost>y.cost; }};priority_queues;int map[205][205],vis[205],n,m;int dijkstra(int st,int ed){ node temp,temp1; temp.pos=st,temp.cost=0; s.push(temp); while(!s.empty()) { temp1=temp=s.top(); s.pop(); if(temp.pos==ed) break; vis[temp.pos]=1; for(int i=0;ix) map[a][b]=map[b][a]=x; } int st,ed; scanf("%d %d",&st,&ed); printf("%d\n",dijkstra(st,ed)); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java String类字符串的理解与认知
下一篇:nyoj757 期末考试 (优先队列)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~