HDOJ 2680 Dijkstra

题目大意:

给你一个有向图,一个起点集合,一个终点,求最短路。。。。

解题思路:

1.自己多加一个超级源点,把起点集合连接到超级源点上,然后将起点与超级源点的集合的路径长度设为0,这样就称为一个n+1个点的单源最短路算法。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int map[1005][1005];
 5 int n;
 6 
 7 int Dijkstra(int s,int e){
 8     bool done[1005];
 9     int d[1005];
10     memset(done,0,sizeof(done));
11     for(int i = 0;i <= n;i++)
12         d[i] = (i == s ? 0 : 1000000);
13     for(int i = 0;i <= n;i++){//最多执行n+1次操作
14         int minx,minn = 1000000;
15         for(int j = 0;j <= n;j++)//先找到d[]最小的点
16             if(!done[j] && d[j] < minn){
17                 minn = d[j];
18                 minx = j;
19             }
20         done[minx] = 1;//将该点加入集合
21         if(minx == e)
22             return d[minx];
23         for(int j = 0;j <= n;j++){//再更新所有的d[]
24             if(!done[j] && d[minx] + map[minx][j] < d[j]){
25                 d[j] = d[minx] + map[minx][j];
26             }
27         }
28     }
29     return -1;//如果没有找到到终点的路径,返回-1
30 }
31 
32 int main(){
33     int m,s;
34     int i,j,k;
35     int p,q,t;
36     int w,ww,ans;
37 
38     while(scanf("%d%d%d",&n,&m,&s) != EOF){
39         ans = 100000000;
40         for(i = 0;i < 1005;i++){
41             for(j = 0;j < 1005;j++){
42                 if(i == j)
43                     map[i][j] = 0;
44                 else
45                     map[i][j] = 1000000;
46             }
47         }
48         
49         while(m--){
50             scanf("%d%d%d",&p,&q,&t);
51             if(t < map[p][q])//可能两站间存在多条线路取短的那条路
52                 map[p][q] = t;
53         }
54         scanf("%d",&w);
55         while(w--){
56             scanf("%d",&ww);
57             map[0][ww] = 0;
58         }
59         ans = Dijkstra(0,s);//巧妙之处,加入超级源点0
60         if(ans == -1)
61             printf("-1
");
62         else
63             printf("%d
",ans);
64     }
65     return 0;
66 }

Choose the best route

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size:

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
原文地址:https://www.cnblogs.com/wushuaiyi/p/3647246.html