首页 > 代码库 > hdu 2680 Choose the best route (dijkstra算法 最短路问题)
hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7201 Accepted Submission(s): 2350
Problem Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
Sample Input
5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211
Sample Output
1-1
题目大意:这个题目的变量比较多,所以比较恶心啦。不过也就是dijkstra的模板问题~
也是给了起点和终点然后找出最短路。这里有一个小技巧就是虚拟起点为"0",把0到任一个真正的起点的距离置为0,到其他点为无穷大,这样可以节省很多时间!!
还有一个要注意的就是这个路是单向的,今天刚过了英语四级,看着英文题目觉得还可以了哈~(*^__^*) 嘻嘻……
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int node[1010],map[1010][1010],n,Min; 6 const int INF=9999999; 7 8 void dijkstra() 9 {10 int vis[1010]= {0};11 int tm=0,m;12 node[tm]=0;13 vis[tm]=1;14 for(int i = 1; i <= n; i++)15 node[i] = INF;16 for (int k=2; k<=n; k++)17 {18 Min=INF;19 for (int i=1; i<=n; i++)20 if (!vis[i])21 {22 if (node[i]>map[tm][i]+node[tm])23 node[i]=map[tm][i]+node[tm];24 if (Min>node[i])25 {26 Min=node[i];27 m=i;28 }29 }30 vis[m]=1;31 tm=m;32 }33 }34 35 int main ()36 {37 int m,s;38 while (~scanf("%d%d%d",&n,&m,&s))39 {40 memset(map, INF, sizeof(map));41 for (int i=1; i<=m; i++)42 {43 int p,q,t;44 cin>>p>>q>>t;45 if (map[p][q]>t)46 map[p][q]=t;47 }48 int w,cost;49 cin>>w;50 for (int i=1; i<=w; i++)51 {52 scanf ("%d",&cost);53 map[0][cost]=0;54 }55 dijkstra();56 if(node[s] ==INF)57 printf("-1\n");58 else59 printf("%d\n", node[s]);60 }61 return 0;62 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。