hdu-2680 Choose the best route---dijkstra+reverse map or create a super source point

Topic link:

http://acm.hdu.edu.cn/showproblem.php?pid=2680

Topic meaning:

Give you a directed graph, a set of starting points and an ending point, find the shortest path

Problem solving ideas:

1. Add one more super source point by yourself, connect the starting point set to the super source point, and then set the path length of the set between the starting point and the super source point to 0, which is called a single-source shortest path with n+1 points algorithm. . . . .

2. Reverse graph + Dijkstra at the end point, then record the minimum value.

Note: heavy edge processing

Idea 1:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1000 + 10;
 5 const int INF = 0x3f3f3f3f;
 6 int Map[maxn][maxn];
 7 int n, m, s;
 8 int d[maxn], v[maxn];
 9 void dijkstra()
10 {
11     memset(v, 0, sizeof(v));
12     for(int i = 0; i <= n; i++)d[i] = INF;
13     d[0] = 0;//源点是0
14     for(int i = 0; i <= n; i++)//n+1个点,循环n+1次
15     {
16         int x = 0, m = INF;
17         for(int j = 1; j <= n; j++)if(!v[j] && d[j] < m)m = d[x = j];
18         v[x] = 1;
19         for(int j = 1; j <= n; j++)
20         {
21             d[j] = min(d[j], d[x] + Map[x][j]);
22         }
23     }
24     if(d[s] == INF)cout<<"-1"<<endl;
25     else cout<<d[s]<<endl;
26 }
27 int main()
28 {
29     while(cin >> n >> m >> s)
30     {
31         int u, v, w;
32         memset(Map, INF, sizeof(Map));
33         while(m--)
34         {
35             scanf("%d%d%d", &u, &v, &w);
36             Map[u][v] = min(Map[u][v], w);//注意重边
37         }
38         scanf("%d", &w);
39         while(w--)
40         {
41             scanf("%d", &u);
42             Map[0][u] = 0; // Create super source 0 
43          }
 44          dijkstra();
 45      }
 46      return  0 ;
 47 }

Idea 2:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1000 + 10;
 5 const int INF = 0x3f3f3f3f;
 6 int Map[maxn][maxn];
 7 int n, m, s;
 8 int d[maxn], v[maxn];
 9 void dijkstra()
10 {
11     memset(v, 0, sizeof(v));
12     for(int i = 0; i <= n; i++)d[i] = INF;
13     d[s] = 0;
14     for(int i = 0; i < n; i++)
15     {
16         int x = 0, m = INF;
17         for(int j = 1; j <= n; j++)if(!v[j] && d[j] < m)m = d[x = j];
18         v[x] = 1;
19         for(int j = 1; j <= n; j++)
20         {
21             d[j] = min(d[j], d[x] + Map[x][j]);
22         }
23     }
24 }
25 int main()
26 {
27     while(cin >> n >> m >> s)
28     {
29         int u, v, w;
30         memset(Map, INF, sizeof(Map));
31         while(m--)
32         {
33             scanf("%d%d%d", &u, &v, &w);
34             Map[v][u] = min(Map[v][u], w);//反向建图
35         }
36         dijkstra();
37         scanf("%d", &w);
38         int ans = INF;
39         while(w--)
40         {
41             scanf("%d", &u);
42             ans = min(ans, d[u]);
43         }
44         if(ans == INF)ans = -1;
45         printf("%d\n " , years);
 46      }
 47      return  0 ;
 48 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324676142&siteId=291194637