###### hdu 2680 最短路

Choose the best route

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 12423    Accepted Submission(s): 4039

Problem Description

One day , Kiki wants to visit one ofher friends. As she is liable to carsickness , she wants to arrive at herfriend’s home as soon as possible . Now give you a map of the city’s trafficroute, and the stations which are near Kiki’s home so that she can take. Youmay suppose Kiki can change the bus at any station. Please find out the leasttime Kiki needs to spend. To make it easy, if the city have n bus stations ,thestations 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 thenumber of directed ways between bus stations .(Maybe there are several waysbetween two bus stations .) s stands for the bus station that near Kiki’sfriend’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 willcosts t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kikican take at the beginning. Then follows w integers stands for these stations.

Output

The output contains one line for eachdata set : the least time Kiki needs to spend ,if it’s impossible to find sucha route ,just output “-1”.

Sample Input

58 5

12 2

15 3

13 4

24 7

25 6

23 5

35 1

45 1

2

23

43 4

12 3

13 4

23 2

1

1

Sample Output

1

-1

Author

dandelion

Source

2009浙江大学计算机研考复试（机试部分）——全真模拟

AC代码：

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define MAX 2000
using namespace std;
int vis[MAX],dis[MAX],map[MAX][MAX];
int n,m,s;
void dij()
{
memset(dis,0,sizeof(dis));
for(int i = 0; i <= n; i++)
vis[i] = map[0][i];
dis[0] = 1;
int min,k;
for(int i = 0; i <= n; i++)
{
min = INF;
for(int j = 0; j <= n; j++)
{
if(!dis[j]&&vis[j] < min)
{
min = vis[j];
k = j;
}
}
if(min == INF)
break;
dis[k] = 1;
for(int j = 0; j <= n; j++)
{
if(dis[j]==0&&vis[j] > vis[k] + map[k][j])
{
vis[j] = vis[k] + map[k][j];
}
}
}
}
int main()
{
int p,q,t;
while(~scanf("%d%d%d",&n,&m,&s))
{
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
map[i][j] = INF;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&p,&q,&t);
if(t < map[p][q])
map[p][q] = t;
}
int w,h;
scanf("%d",&w);
for(int i = 1; i <= w; i++)
{
scanf("%d",&h);
map[0][h] = 0;
}
/*for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= n; j++)
printf("%d ",map[i][j]);
printf("\n");
}*/
dij();
if(vis[s]!=INF)
printf("%d\n",vis[s]);
else
printf("-1\n");
}
return 0;
}


#### hdu2680（最短路spfa！此题有重大发现）

2015-10-15 18:36:41

#### 典型的dijkstra HDU 2680 Choose the best route

2012-05-17 23:30:52

#### HDU2680：Choose the best route(Dijkstra)

2013-11-26 20:38:43

#### hdu 2680

2017-09-22 21:41:17

#### HDU-2544 最短路【最短路】

2012-02-25 09:31:02

#### hdu oj 2544 最短路(最短路径）

2014-08-10 15:16:25

#### 最短路习题集（hdu）

2016-08-05 10:22:07

#### HDU 2680 最短路

2015-10-14 16:49:08

#### HDU-2433 Travel（最短路[Dijkstra]）

2015-08-14 16:14:09

#### 最短路径 贪心实现代码

2009年09月06日 2KB 下载

hdu 2680 最短路