HDU 2680 Choose the best route【最短路】
2017-11-12 13:18
537 查看
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16156 Accepted Submission(s): 5265
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.
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 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1
Sample Output
1-1
题目描述:
给你KIKI家附近的公交站,和KIKI朋友家的标号,问KIKI到朋友家最少需要多少时间。解题分析:
因为公交站的序号是从1开始的,所以可以假设KIKI家的序号为0,记从KIKI家到附近的公交站所花费的时间为0,再用dijkstra跑一边,最后输出的结果就是到KIKI朋友家的最少时间。AC代码:
#include<stdio.h> #include<string.h> #define N 1005 #define inf 99999999 int dis , e , book ; void dijkstra(int n) { int i, j = inf, k, Min; memset(book, 0, sizeof(book)); for(i = 1; i <= n; i++) dis[i] = e[0][i]; book[0] = 1; for(k = 0; k < n; k++){ Min = inf; for(i = 1; i <= n; i++) if(!book[i] && dis[i] < Min) j = i, Min = dis[i]; if(j == inf) break; book[j] = 1; for(i = 1; i <= n; i++) if(!book[i] && dis[i] > dis[j]+e[j][i]) dis[i] = dis[j]+e[j][i]; } } int main() { int i, j, n, m, s, u, v, w; while(~scanf("%d%d%d", &n, &m, &s)){ for(i = 0; i <= n; i++){ for(j = 1; j <= n; j++) e[i][j] = inf; e[i][i] = 0; } while(m--){ scanf("%d%d%d", &u, &v, &w); if(e[u][v] > w) e[u][v] = w; } scanf("%d", &m); while(m--){ scanf("%d", &u); e[0][u] = 0; } dijkstra(n); printf("%d\n", dis[s]==inf?-1:dis[s]); } return 0; }
相关文章推荐
- hdu 2680 Choose the best route(最短路)
- HDU 2680Choose the best route(dijk最短路)
- hdu 2680 Choose the best route(最短路dijkstra(有重边))
- HDU-2680 Choose the best route(最短路[Dijkstra])
- hdu 2680 Choose the best route 最短路
- HDU 2680 Choose the best route 最短路(Dijkstra算法)
- HDU 2680 Choose the best route(最短路)
- hdu 2680 Choose the best route(最短路)
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- HDU 2680 Choose the best route 最短路
- hdu-2680 Choose the best route(最短路)
- HDU 2680 Choose the best route 最短路
- hdu 2680-Choose the best route最短路dijkstra
- HDU 2680 Choose the best route (最短路+反向图)
- HDU ~ 2680 ~ Choose the best route (反向建边 or 超级源点 + 最短路)
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- HDU 2680 Choose the best route (最短路+迪杰斯特拉)
- HDU 2680 Choose the best route(最短路,反向)
- HdU2680——Choose the best route(单源最短路Bell man-ford)