[kuangbin]带你飞之'最短路练习'专题(未完成)

// 带飞网址 ฅʕ•̫͡•ʔฅ

 

专题四 最短路练习 
√ POJ 2387 Til the Cows Come Home
√ POJ 2253 Frogger
√ POJ 1797 Heavy Transportation
√ POJ 3268 Silver Cow Party
√ POJ 1860 Currency Exchange
√ POJ 3259 Wormholes
√ POJ 1502 MPI Maelstrom
√  POJ 3660 Cow Contest
√ POJ 2240 Arbitrage
√ POJ 1511 Invitation Cards
POJ 3159 Candies
POJ 2502 Subway
POJ 1062 昂贵的聘礼
POJ 1847 Tram
LightOJ 1074 Extended Traffic
HDU 4725 The Shortest Path in Nya Graph
HDU 3416 Marriage Match IV
HDU 4370 0 or 1
POJ 3169 Layout

 

// poj 2387

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-25 08:14:08
 7  * @LastEditTime: 2019-10-25 08:25:39
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include<string>
14 #include
15 #include
16 #include
17 #include
18 #include<set>
19 #include
20 #define rep(i, n) for(int i=0;i!=n;++i)
21 #define per(i, n) for(int i=n-1;i>=0;--i)
22 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
23 #define rep1(i, n) for(int i=1;i<=n;++i)
24 #define per1(i, n) for(int i=n;i>=1;--i)
25 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
26 #define L k<<1
27 #define R k<<1|1
28 #define mid (tree[k].l+tree[k].r)>>1
29 using namespace std;
30 const int INF = 0x3f3f3f3f;
31 typedef long long ll;
32 
33 inline int read() {
34     char c=getchar();int x=0,f=1;
35     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
36     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
37     return x*f;
38 }
39 // -----------------------------------------------------
40 const int MAXN = 1000+5;
41 int T, n;
42 int G[MAXN][MAXN];
43 
44 int vis[MAXN], dis[MAXN];
45 
46 int dij() {
47     rep1(i, n) {
48         dis[i] = G[i][1];
49         vis[i] = 0;
50     }
51     vis[1] = 1;
52     rep(i, n-1) {
53         int minn = INF;
54         int t;
55         rep1(j, n) {
56             if(!vis[j] && dis[j] < minn) {
57                 minn = dis[j];
58                 t = j;
59             }
60         }
61         vis[t] = 1;
62         rep1(j, n) {
63             if(!vis[j] && dis[j] > dis[t] + G[t][j]) {
64                 dis[j] = dis[t] + G[t][j];
65             }
66         }
67     }
68     return dis[n];
69 }
70 
71 int main() {
72     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
73     int s, e, v;
74     while(cin >> T >> n) {
75         rep1(i, n) rep1(j, n) G[i][j] = INF;
76         rep(i, T) {
77             cin >> s >> e >> v;
78             if(v < G[s][e]) G[s][e] = G[e][s] = v;
79         }
80         cout << dij() << "\n";
81     }
82     return 0;
83 }
View Code

 // poj 2253

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-25 09:19:11
 7  * @LastEditTime: 2019-10-25 10:28:41
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include
14 #include<string>
15 #include
16 #include
17 #include
18 #include
19 #include<set>
20 #include
21 #define rep(i, n) for(int i=0;i!=n;++i)
22 #define per(i, n) for(int i=n-1;i>=0;--i)
23 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
24 #define rep1(i, n) for(int i=1;i<=n;++i)
25 #define per1(i, n) for(int i=n;i>=1;--i)
26 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
27 #define L k<<1
28 #define R k<<1|1
29 #define mid (tree[k].l+tree[k].r)>>1
30 using namespace std;
31 const int INF = 0x3f3f3f3f;
32 typedef long long ll;
33 
34 inline int read() {
35     char c=getchar();int x=0,f=1;
36     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
37     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
38     return x*f;
39 }
40 // -----------------------------------------------------
41 const int MAXN = 200+5;
42 
43 int n;
44 int x[MAXN], y[MAXN];
45 double G[MAXN][MAXN];
46 
47 bool vis[MAXN];
48 double dis[MAXN];
49 
50 double dij() {
51     rep(i, n) {
52         vis[i] = false;
53         dis[i] = G[i][0];
54     }
55     vis[0] = 1;
56     rep(i, n-1) {
57         double minn = INF;
58         int t;
59         rep(j, n) {
60             if(!vis[j] && dis[j] < minn) {
61                 minn = dis[j];
62                 t = j;
63             }
64         }
65         vis[t] = true;
66         rep(j, n) {
67             if(!vis[j]) {
68                 dis[j] = min(dis[j], max(minn, G[t][j]));
69             }
70         }
71     }
72     return dis[1];
73 }
74 
75 int main() {
76     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
77     int cnt = 1;
78     while(cin >> n && n) {
79         rep(i, n) x[i] = read(), y[i] = read();
80         rep(i, n) Rep(j, i, n) {
81             if(i == j) G[i][i] = INF;
82             else G[i][j] = G[j][i] = sqrt(double((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
83         }
84         cout << "Scenario #" << cnt++ << "\n";
85         //printf("Frog Distance = %.3f\n\n", dij());
86         cout << "Frog Distance = " << fixed << setprecision(3) << dij() << "\n\n";
87     }
88     return 0;
89 }
View Code

 // poj 1797

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-25 11:00:57
 7  * @LastEditTime: 2019-10-25 14:04:36
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include<string>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include<set>
20 #include
21 #define rep(i, n) for(int i=0;i!=n;++i)
22 #define per(i, n) for(int i=n-1;i>=0;--i)
23 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
24 #define rep1(i, n) for(int i=1;i<=n;++i)
25 #define per1(i, n) for(int i=n;i>=1;--i)
26 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
27 #define L k<<1
28 #define R k<<1|1
29 #define mid (tree[k].l+tree[k].r)>>1
30 using namespace std;
31 const int INF = 0x3f3f3f3f;
32 typedef long long ll;
33 
34 inline int read() {
35     char c=getchar();int x=0,f=1;
36     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
37     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
38     return x*f;
39 }
40 // -----------------------------------------------------
41 const int MAXN = 1000+5;
42 
43 int n, m;
44 int G[MAXN][MAXN], dis[MAXN];
45 bool vis[MAXN];
46 
47 int dij() {
48     rep1(i, n) {
49         dis[i] = G[i][1];
50         vis[i] = false;
51     }
52     vis[1] = true;
53     rep(i, n-1) {
54         int maxnn = 0;
55         int t = -1;
56         rep1(j, n) {
57             if(!vis[j] && dis[j] > maxnn) { //!vis[j] && dis[j] != INF && dis[j] > maxnn
58                 maxnn = dis[j];
59                 t = j;
60             }
61         }
62         if(t == -1) break;
63         vis[t] = true;
64         rep1(j, n) {
65             if(!vis[j]) {
66                 dis[j] = max(dis[j], min(maxnn, G[t][j]));
67             }
68         }
69     }
70     return dis[n];
71 }
72 
73 int main() {
74     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
75     int T = read();
76     int cnt = 1;
77     int s, e, v;
78     while(T--) {
79         n = read(); m = read();
80         rep1(i, n) rep1(j, n) G[i][j] = 0; //G[i][j] = INF;
81         rep(i, m) {
82             s = read(); e = read(); v = read();
83             G[s][e] = G[e][s] = v;
84         }
85         cout << "Scenario #" << cnt++ << ":" << "\n";
86         cout << dij() << "\n\n";
87     }
88     return 0;
89 }
View Code

 // poj 3268

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-25 14:25:52
 7  * @LastEditTime: 2019-10-25 15:51:43
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include<string>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include<set>
20 #include
21 #define read(n) n = input_i()
22 #define rep(i, n) for(int i=0;i!=n;++i)
23 #define per(i, n) for(int i=n-1;i>=0;--i)
24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
25 #define rep1(i, n) for(int i=1;i<=n;++i)
26 #define per1(i, n) for(int i=n;i>=1;--i)
27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
28 #define L k<<1
29 #define R k<<1|1
30 #define mid (tree[k].l+tree[k].r)>>1
31 using namespace std;
32 const int INF = 0x3f3f3f3f;
33 typedef long long ll;
34 
35 inline int input_i() {
36     char c=getchar();int x=0,f=1;
37     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
38     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
39     return x*f;
40 }
41 // -----------------------------------------------------
42 const int MAXN = 1000+5;
43 const int MAXM = 100000+5;
44 
45 int n, m, x;
46 int s[MAXM], e[MAXM], v[MAXM];
47 int G[MAXN][MAXN];
48 
49 int dis[MAXN];
50 int a1[MAXN], a2[MAXN];
51 bool vis[MAXN];
52 
53 void dij() {
54     rep1(i, n) {
55         vis[i] = false;
56         dis[i] = G[x][i];
57     }
58     vis[x] = true;
59     rep(i, n-1) {
60         int minn = INF;
61         int t = -1;
62         rep1(j, n) {
63             if(!vis[j] && dis[j] < minn) {
64                 minn = dis[j];
65                 t = j;
66             }
67         }
68         if(t == -1) break;
69         vis[t] = true;
70         rep1(j, n) {
71             if(!vis[j] && G[t][j] != INF && dis[j] > minn + G[t][j]) {
72                 dis[j] = minn + G[t][j];
73             }
74         }
75     }
76 }
77 
78 int main() {
79     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
80     while(cin >> n >> m >> x) {
81         rep1(i, n) rep1(j, n) G[i][j] = INF;
82         rep(i, m) {
83             read(s[i]); read(e[i]); read(v[i]);
84             if(v[i] < G[s[i]][e[i]]) G[s[i]][e[i]] = v[i];
85         }
86         dij();
87         rep1(i, n) a1[i] = dis[i];
88         rep1(i, n) rep1(j, n) G[i][j] = INF;
89         rep(i, m) {
90             if(v[i] < G[e[i]][s[i]]) G[e[i]][s[i]] = v[i];
91         }
92         dij();
93         rep1(i, n) a2[i] = dis[i];
94         int maxn = -1;
95         rep1(i, n) if(a1[i] != INF && a2[i] != INF && a1[i]+a2[i] > maxn) maxn = a1[i]+a2[i];
96         cout << maxn << "\n";
97     }
98     return 0;
99 }
View Code

 // poj 1860

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-25 16:51:13
 7  * @LastEditTime: 2019-10-25 20:25:55
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include<string>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include<set>
20 #include
21 #define read(n) n = read_n()
22 #define rep(i, n) for(int i=0;i!=n;++i)
23 #define per(i, n) for(int i=n-1;i>=0;--i)
24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
25 #define rep1(i, n) for(int i=1;i<=n;++i)
26 #define per1(i, n) for(int i=n;i>=1;--i)
27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
28 #define L k<<1
29 #define R k<<1|1
30 #define mid (tree[k].l+tree[k].r)>>1
31 #define eps 1e-10
32 using namespace std;
33 const int INF = 0x3f3f3f3f;
34 typedef long long ll;
35 
36 inline int read_n() {
37     char c=getchar();int x=0,f=1;
38     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
39     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
40     return x*f;
41 }
42 // -----------------------------------------------------
43 const int MAXN = 100+5;
44 const int MAXM = 200+5;
45 
46 int n, m, m2, x;
47 double sta;
48 
49 struct node {
50     int u, v;
51     double r, c;
52 } edge[MAXM];
53 
54 double dis[MAXN];
55 
56 bool Bellman_ford() {
57     bool flag;
58     rep(i, n-1) {
59         flag = false;
60         rep(j, m) {
61             if(dis[edge[j].v] < (dis[edge[j].u]-edge[j].c)*edge[j].r) {
62                 dis[edge[j].v] = (dis[edge[j].u]-edge[j].c)*edge[j].r;
63                 flag = true;
64             }
65         }
66         if(!flag) break;
67     }
68     flag = false;
69     rep(i, m) {
70         if(dis[edge[i].v] < (dis[edge[i].u]-edge[i].c)*edge[i].r) {
71             return true;
72         }
73     }
74     return false;
75 }
76 
77 int main() {
78     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
79     cin >> n >> m2 >> x >> sta;
80     m = 0;
81     rep(i, m2) {
82         cin >> edge[m].u >> edge[m].v;
83         cin >> edge[m].r >> edge[m].c;
84         ++m;
85         edge[m].u = edge[m-1].v;
86         edge[m].v = edge[m-1].u;
87         cin >> edge[m].r >> edge[m].c;
88         ++m;
89     }
90     rep1(i, n) dis[i] = 0;
91     dis[x] = sta;
92     if(Bellman_ford()) cout << "YES\n";
93     else cout << "NO\n";
94     return 0;
95 }
View Code

 // poj 3259

  1 /*
  2  * @Promlem: 
  3  * @Time Limit: ms
  4  * @Memory Limit: k
  5  * @Author: pupil-XJ
  6  * @Date: 2019-10-26 00:52:36
  7  * @LastEditTime: 2019-10-26 01:40:33
  8  */
  9 #include
 10 #include
 11 #include
 12 #include
 13 #include<string>
 14 #include
 15 #include
 16 #include
 17 #include
 18 #include
 19 #include<set>
 20 #include
 21 #define read(n) n = read_n()
 22 #define rep(i, n) for(int i=0;i!=n;++i)
 23 #define per(i, n) for(int i=n-1;i>=0;--i)
 24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
 25 #define rep1(i, n) for(int i=1;i<=n;++i)
 26 #define per1(i, n) for(int i=n;i>=1;--i)
 27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
 28 #define L k<<1
 29 #define R k<<1|1
 30 #define mid (tree[k].l+tree[k].r)>>1
 31 #define eps 1e-10
 32 using namespace std;
 33 const int INF = 0x3f3f3f3f;
 34 typedef long long ll;
 35 
 36 inline int read_n() {
 37     char c=getchar();int x=0,f=1;
 38     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
 39     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
 40     return x*f;
 41 }
 42 // -----------------------------------------------------
 43 const int MAXN = 500+5;
 44 const int MAXM = 5200+5;
 45 
 46 int n, m1, m2;
 47 
 48 int num;
 49 struct node {
 50     int v, next;
 51     int w;
 52 } edge[MAXM];
 53 
 54 int head[MAXN];
 55 inline void add(int x, int y, int w) {
 56     edge[num].v = y;
 57     edge[num].next = head[x];
 58     edge[num].w = w;
 59     head[x] = num++;
 60 }
 61 
 62 int dis[MAXN], vis[MAXN], inq[MAXN];
 63 
 64 bool SPFA(int s) {
 65     queue<int> q;
 66     rep1(i, n) {
 67         dis[i] = INF;
 68         vis[i] = inq[i] = 0;
 69     }
 70     dis[s] = 0;
 71     vis[s] = inq[s] = 1;
 72     q.push(s);
 73     while(!q.empty()) {
 74         int u = q.front();
 75         q.pop();
 76         vis[u] = 0;
 77         for(int i = head[u]; i != -1; i = edge[i].next) {
 78             int v = edge[i].v;
 79             if(dis[v] > dis[u] + edge[i].w) {
 80                 dis[v] = dis[u] + edge[i].w;
 81                 if(!vis[v]) {
 82                     q.push(v);
 83                     vis[v] = 1;
 84                     ++inq[v];
 85                     if(inq[v] > n) return true;
 86                 }
 87             }
 88         }
 89     }
 90     return false;
 91 }
 92 
 93 int main() {
 94     //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 95     int T = read_n();
 96     int s, e, v;
 97     while(T--) {
 98         read(n); read(m1); read(m2);
 99         rep1(i, n) head[i] = -1;
100         num = 0;
101         rep(i, m1) {
102             read(s); read(e); read(v);
103             add(s, e, v);
104             add(e, s, v);
105         }
106         rep(i, m2) {
107             read(s); read(e); read(v);
108             add(s, e, -v);
109         }
110         if(SPFA(1)) cout << "YES\n";
111         else cout << "NO\n";
112     }
113     return 0;
114 }
View Code

 // poj 1502

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-26 15:10:47
 7  * @LastEditTime: 2019-10-26 15:50:27
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include<string>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include<set>
20 #include
21 #define read(n) n = read_n()
22 #define rep(i, n) for(int i=0;i!=n;++i)
23 #define per(i, n) for(int i=n-1;i>=0;--i)
24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
25 #define rep1(i, n) for(int i=1;i<=n;++i)
26 #define per1(i, n) for(int i=n;i>=1;--i)
27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
28 #define L k<<1
29 #define R k<<1|1
30 #define mid (tree[k].l+tree[k].r)>>1
31 #define eps 1e-10
32 using namespace std;
33 const int INF = 0x3f3f3f3f;
34 typedef long long ll;
35 
36 inline int read_n() {
37     char c=getchar();int x=0,f=1;
38     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
39     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
40     return x*f;
41 }
42 // -----------------------------------------------------
43 const int MAXN = 100+5;
44 int n;
45 int G[MAXN][MAXN];
46 
47 int dis[MAXN], vis[MAXN];
48 
49 int dij() {
50     rep1(i, n) {
51         dis[i] = G[i][1];
52         vis[i] = 0;
53     }
54     vis[1] = 1;
55     rep(i, n-1) {
56         int minn = INF;
57         int t = -1;
58         rep1(j, n) if(!vis[j] && dis[j] < minn) {
59             t = j;
60             minn = dis[j];
61         }
62         if(t == -1) break;
63         vis[t] = 1;
64         rep1(j, n) if(!vis[j] && dis[j] > dis[t] + G[t][j]) {
65             dis[j] = dis[t] + G[t][j];
66         }
67     }
68     int maxn = 0;
69     rep1(i, n) if(dis[i] != INF && dis[i] > maxn) maxn = dis[i];
70     return maxn;
71 }
72 
73 int main() {
74     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
75     cin >> n;
76     rep1(i, n) rep1(j, n) G[i][j] = INF;
77     int x;
78     Rep1(i, 2, n) {
79         Rep(j, 1, i) {
80             string num;
81             cin >> num;
82             if(num == "x") continue;
83             else {
84                 int x = 0;
85                 rep(k, num.size()) x = x*10 + num[k]-'0';
86                 G[i][j] = G[j][i] = x;
87             }
88         }
89     }
90     cout << dij() << "\n";
91     return 0;
92 }
View Code

 // poj 3660

 1 /*
 2  * @Promlem: 
 3  * @Time Limit: ms
 4  * @Memory Limit: k
 5  * @Author: pupil-XJ
 6  * @Date: 2019-10-26 16:15:10
 7  * @LastEditTime: 2019-10-26 17:00:11
 8  */
 9 #include
10 #include
11 #include
12 #include
13 #include<string>
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include<set>
20 #include
21 #define read(n) n = read_n()
22 #define rep(i, n) for(int i=0;i!=n;++i)
23 #define per(i, n) for(int i=n-1;i>=0;--i)
24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
25 #define rep1(i, n) for(int i=1;i<=n;++i)
26 #define per1(i, n) for(int i=n;i>=1;--i)
27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
28 #define L k<<1
29 #define R k<<1|1
30 #define mid (tree[k].l+tree[k].r)>>1
31 #define eps 1e-10
32 using namespace std;
33 const int INF = 0x3f3f3f3f;
34 typedef long long ll;
35 
36 inline int read_n() {
37     char c=getchar();int x=0,f=1;
38     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
39     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
40     return x*f;
41 }
42 // -----------------------------------------------------
43 const int MAXN = 100+5;
44 const int MAXM = 4500+5;
45 
46 int n, m;
47 int dis[MAXN];
48 vector<int> a[MAXN], b[MAXN];
49 int vis[MAXN], ans;
50 
51 void dfs(vector<int> edge[MAXN], int x) {
52     vis[x] = 1;
53     ++ans;
54     if(!edge[x].size()) return;
55     for(int i = 0; i != edge[x].size(); ++i) {
56         if(!vis[edge[x][i]]) dfs(edge, edge[x][i]);
57     }
58 }
59 
60 int main() {
61     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
62     cin >> n >> m;
63     rep1(i, n) dis[i] = 0;
64     int x, y;
65     rep(i, m) {
66         cin >> x >> y;
67         a[x].push_back(y);
68         b[y].push_back(x);
69     }
70     int cnt = 0;
71     rep1(i, n) {
72         ans = 0;
73         rep1(j, n) vis[j] = 0;
74         dfs(a, i);
75         rep1(j, n) vis[j] = 0;
76         dfs(b, i);
77         if(ans == n+1) ++cnt;
78     }
79     cout << cnt << "\n";
80     return 0;
81 }
View Code

 // poj 2240

  1 /*
  2  * @Promlem: 
  3  * @Time Limit: ms
  4  * @Memory Limit: k
  5  * @Author: pupil-XJ
  6  * @Date: 2019-10-26 17:25:13
  7  * @LastEditTime: 2019-10-26 18:57:21
  8  */
  9 #include
 10 #include
 11 #include
 12 #include
 13 #include<string>
 14 #include
 15 #include
 16 #include
 17 #include
 18 #include
 19 #include<set>
 20 #include
 21 #define read(n) n = read_n()
 22 #define rep(i, n) for(int i=0;i!=n;++i)
 23 #define per(i, n) for(int i=n-1;i>=0;--i)
 24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
 25 #define rep1(i, n) for(int i=1;i<=n;++i)
 26 #define per1(i, n) for(int i=n;i>=1;--i)
 27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
 28 #define L k<<1
 29 #define R k<<1|1
 30 #define mid (tree[k].l+tree[k].r)>>1
 31 #define eps 1e-10
 32 using namespace std;
 33 const int INF = 0x3f3f3f3f;
 34 typedef long long ll;
 35 
 36 inline int read_n() {
 37     char c=getchar();int x=0,f=1;
 38     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
 39     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
 40     return x*f;
 41 }
 42 // -----------------------------------------------------
 43 const int MAXN = 30+5;
 44 typedef pair<int, double> Pair;
 45 
 46 int n, m;
 47 map<string, int> id;
 48 vector edge[MAXN];
 49 
 50 int vis[MAXN], inq[MAXN];
 51 double dis[MAXN];
 52 
 53 bool SPFA() {
 54     dis[0] = 10.0;
 55     rep1(i, n) dis[i] = 0, inq[i] = vis[i] = 0;
 56     vis[0] = inq[0] = 1;
 57     queue<int> q;
 58     q.push(0);
 59     while(!q.empty()) {
 60         int u = q.front();
 61         q.pop();
 62         vis[u] = 0;
 63         rep(i, edge[u].size()) {
 64             Pair v = edge[u][i];
 65             if(dis[v.first] < dis[u]*v.second) {
 66                 dis[v.first] = dis[u]*v.second;
 67                 if(!vis[v.first]) {
 68                     vis[v.first] = 1;
 69                     q.push(v.first);
 70                     if(++inq[v.first] > n) return true;
 71                 }
 72             }
 73         }
 74     }
 75     return false;
 76 }
 77 
 78 int main() {
 79     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 80     int Case = 0;
 81     while(cin >> n && n) {
 82         Rep1(i, 0, n) edge[i].clear();
 83         int cnt = 1;
 84         string name;
 85         rep(i, n) {
 86             cin >> name;
 87             Pair p = make_pair(cnt, 10.0);
 88             edge[0].push_back(p);
 89             id[name] = cnt++;
 90         }
 91         cin >> m;
 92         string u, v;
 93         double r;
 94         rep(i, m) {
 95             cin >> u >> r >> v;
 96             Pair p = make_pair(id[v], r);
 97             edge[id[u]].push_back(p);
 98         }
 99         if(SPFA()) cout << "Case " << ++Case << ": Yes\n";
100         else cout << "Case " << ++Case << ": No\n";
101     }
102     return 0;
103 }
View Code

 // poj 1511

  1 /*
  2  * @Promlem: 
  3  * @Time Limit: ms
  4  * @Memory Limit: k
  5  * @Author: pupil-XJ
  6  * @Date: 2019-10-27 00:31:47
  7  * @LastEditTime: 2019-10-27 01:10:54
  8  */
  9 #include
 10 #include
 11 #include
 12 #include
 13 #include<string>
 14 #include
 15 #include
 16 #include
 17 #include
 18 #include
 19 #include<set>
 20 #include
 21 #define read(n) n = read_n()
 22 #define rep(i, n) for(int i=0;i!=n;++i)
 23 #define per(i, n) for(int i=n-1;i>=0;--i)
 24 #define Rep(i, sta, n) for(int i=sta;i!=n;++i)
 25 #define rep1(i, n) for(int i=1;i<=n;++i)
 26 #define per1(i, n) for(int i=n;i>=1;--i)
 27 #define Rep1(i, sta, n) for(int i=sta;i<=n;++i)
 28 #define L k<<1
 29 #define R k<<1|1
 30 #define mid (tree[k].l+tree[k].r)>>1
 31 #define eps 1e-10
 32 using namespace std;
 33 const int INF = 0x3f3f3f3f;
 34 typedef long long ll;
 35 
 36 inline int read_n() {
 37     char c=getchar();int x=0,f=1;
 38     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
 39     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
 40     return x*f;
 41 }
 42 // -----------------------------------------------------
 43 const int MAXN = 1000000+5;
 44 const int MAXM = 2000000+5;
 45 typedef pair<int, int> Pair;
 46 
 47 int n, m;
 48 ll sum;
 49 int s[MAXN], e[MAXN], v[MAXN];
 50 
 51 int num;
 52 struct node {
 53     int v, w, next;
 54 } edge[MAXM];
 55 
 56 int head[MAXN];
 57 inline void add(int x, int y, int w) {
 58     edge[num].v = y;
 59     edge[num].w = w;
 60     edge[num].next = head[x];
 61     head[x] = num++;
 62 }
 63 
 64 int dis[MAXN], vis[MAXN];
 65 
 66 void dij() {
 67     priority_queue, greater > q;
 68     q.push(make_pair(0, 1));
 69     while(!q.empty()) {
 70         int u = q.top().second;
 71         q.pop();
 72         if(vis[u]) continue;
 73         vis[u] = 1;
 74         for(int i = head[u]; i != -1; i = edge[i].next) {
 75             if(!vis[edge[i].v] && dis[edge[i].v] > dis[u] + edge[i].w) {
 76                 dis[edge[i].v] = dis[u] + edge[i].w;
 77                 q.push(make_pair(dis[edge[i].v], edge[i].v));
 78             }
 79         }
 80     }
 81 }
 82 
 83 int main() {
 84     //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 85     int T;
 86     scanf("%d", &T); //cin >> T;
 87     while(T--) {
 88         scanf("%d%d", &n, &m); //cin >> n >> m;
 89         int x, y, w;
 90         sum = 0;
 91         num = 1;
 92         rep1(i, n) head[i] = -1;
 93         rep(i, m) {
 94             scanf("%d%d%d", &s[i], &e[i], &v[i]); //cin >> s[i] >> e[i] >> v[i];
 95             add(s[i], e[i], v[i]);
 96         }
 97         rep1(i, n) {
 98             dis[i] = INF;
 99             vis[i] = 0;
100         }
101         dis[1] = 0;
102         dij();
103         rep1(i, n) {
104             sum += dis[i];
105             dis[i] = INF;
106             vis[i] = 0;
107             head[i] = -1;
108         }
109         dis[1] = 0;
110         num = 1;
111         rep(i, m) add(e[i], s[i], v[i]);
112         dij();
113         rep1(i, n) sum += dis[i];
114         printf("%lld\n", sum); //cout << sum << "\n";
115     }
116     return 0;
117 }
View Code

 

你可能感兴趣的:([kuangbin]带你飞之'最短路练习'专题(未完成))