51Nod 1649 齐头并进

Posted jaydenouyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51Nod 1649 齐头并进相关的知识,希望对你有一定的参考价值。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 //两遍迪杰斯特拉
 5 #define INF 0xfffffff
 6 using namespace std;
 7 const int maxn = 400 + 5;
 8 int rail[maxn][maxn];
 9 int dis[maxn];
10 int vis[maxn];
11 
12 void dij(int n, int u){
13     int i, j, p, Min;
14     memset(vis, 0, sizeof(vis));
15     for (int i = 1; i <= n; i++)
16         dis[i] = rail[u][i];
17     vis[u] = 1;
18     dis[u] = 0;
19 
20     for (i = 1; i < n; i++){
21         Min = INF;
22         for (j = 1; j <= n; j++){
23             if (!vis[j] && dis[j] < Min){
24                 p = j;
25                 Min = dis[j];
26             }
27         }
28         if (Min == INF) return;
29         vis[p] = 1;
30         for (j = 1; j <= n; j++){
31             if (!vis[j] && dis[p] + rail[p][j] < dis[j]){
32                 dis[j] = dis[p] + rail[p][j];
33             }
34         }
35     }
36 }
37 
38 int main(){
39     ios::sync_with_stdio(false);
40     fill(rail[0], rail[0] + maxn*maxn, INF);
41     int n, m;
42     cin >> n >> m;
43     if (m == 0){
44         //没有铁路
45         cout << "-1" << endl;
46         return 0;
47     }
48     for (int i = 0; i < m; i++){
49         int x, y;
50         cin >> x >> y;
51         rail[x][y] = 1;
52         rail[y][x] = 1;
53     }
54     dij(n, 1);
55     int ans1 = dis[n];
56     if (ans1 == INF || m == (n*(n - 1) / 2)){
57         //铁路没有答案,或者没有公路
58         cout << "-1" << endl;
59         return 0;
60     }
61 
62     for (int i = 1; i <= n; i++){
63         for (int j = 1 + i; j <= n; j++){
64             if (rail[i][j] == 1){
65                 rail[i][j] = INF;
66                 rail[j][i] = INF;
67             }
68             else{
69                 rail[i][j] = 1;
70                 rail[j][i] = 1;
71             }
72         }
73     }
74     dij(n, 1);
75     int ans2 = dis[n];
76     int ans = ans1 > ans2 ? ans1 : ans2;
77     cout << ans << endl;
78 
79 
80     system("pause");
81     return 0;
82 }

 

以上是关于51Nod 1649 齐头并进的主要内容,如果未能解决你的问题,请参考以下文章

51nod1563

51Nod1039 N^3 Mod P 数论 原根 BSGS

51nod1174(RMQ)

51nod1105(二分)

51nod 1179:最大的最大公约数

区间dp 51nod1021