Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10601    Accepted Submission(s): 3415


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 5
1 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
 

 

Author
dandelion
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1142 1385 2923 1690 2722 
Dij:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int map[1010][1010], dis[1010]; 
 6 bool vis[1010];
 7 const int INF = 0x3f3f3f3f;
 8 int n, m;
 9 void Dijkstra(int a)
10 {
11     memset(vis, true, sizeof(vis));
12     for(int i = 1; i <= n; i++)
13         dis[i] = map[a][i];
14     vis[a] = false; dis[a] = 0;
15     for(int i = 1; i < n; i++)
16     {
17         int temp = -1, min = INF;
18         for(int j = 1; j <= n; j++)
19         {
20             if(vis[j] && dis[j] < min)
21             {
22                 temp = j;
23                 min = dis[j];
24             }
25         }
26         if(temp == -1)
27             break;
28         vis[temp]=false;
29         for(int j = 1; j <= n; j++)
30             if(vis[j] && dis[j] > dis[temp] + map[temp][j])
31                 dis[j] = dis[temp] + map[temp][j];
32     }
33 }
34 int main() 
35 {
36     int s;
37     while(~scanf("%d %d %d", &n, &m, &s))
38     {
39         for(int i = 1; i <= n; i++)
40             for(int j = 1; j <= n; j++)
41                 map[i][j]=INF; 
42         int u, v, w;
43         for(int i = 1; i <= m; i++)
44         { 
45             scanf("%d %d %d", &u, &v, &w);
46             if(map[v][u] > w)   // 反向建图(有向图);  
47                 map[v][u]=w;
48         }
49         Dijkstra(s);
50         int Q, a, min = INF;
51         scanf("%d", &Q);
52         for(int i = 1; i <= Q; i++) 
53         {
54             scanf("%d", &a); 
55             if(dis[a] < min)
56                 min = dis[a];
57         } 
58         if(min == INF)
59             printf("-1\n");
60         else
61             printf("%d\n", min);
62     } 
63     return 0;
64 }

 

 

posted on 2015-08-18 21:38  cleverbiger  阅读(231)  评论(0编辑  收藏  举报