水水更健康。。。话说回来,这真的是水题?T T

首先,容易想到:

令ans1 = t1为源,到s和t2的距离之和;ans2 = t2为源,到s和t1的距离之和

ans = min(ans1, ans2)

然后,开始写单元最短路。。。spfa。。。

 

 1 /**************************************************************
 2     Problem: 2100
 3     User: rausen
 4     Language: C++
 5     Result: Time_Limit_Exceed
 6 ****************************************************************/
 7  
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <algorithm>
12  
13 using namespace std;
14 const int N = 100005;
15 const int M = 400005;
16  
17 struct edges {
18     int next, to, v;
19     edges() {}
20     edges(int _next, int _to, int _v) : next(_next), to(_to), v(_v) {}
21 }e[M];
22  
23 int n, m, s, T1, T2;
24 int first[N], tot;
25 int q[N], l, r, dis[N];
26 bool v[N];
27 int ans;
28  
29 inline int read() {
30     int x = 0;
31     char ch = getchar();
32     while (!isdigit(ch))
33         ch = getchar();
34     while (isdigit(ch)) {
35         x = x * 10 + ch - '0';
36         ch = getchar();
37     }
38     return x;
39 }
40  
41 inline void Add_Edges(int x, int y, int z) {
42     e[++tot] = edges(first[x], y, z), first[x] = tot;
43     e[++tot] = edges(first[y], x, z), first[y] = tot;
44 }
45  
46 void spfa(int S) {
47     memset(dis, 127 / 3, sizeof(dis));
48     q[0] = S, dis[S] = 0, v[S] = 1;
49     int p, x, y;
50     for (l = 0, r = 0; l != (r + 1) % N; (++l) %= N) {
51         p = q[l];
52         for (x = first[p]; x; x = e[x].next)
53             if (dis[p] + e[x].v < dis[(y = e[x].to)]) {
54                 dis[y] = dis[p] + e[x].v;
55                 if (!v[y])
56                     v[y] = 1, q[(++r) %= N] = y;
57             }
58         v[p] = 0;
59     }
60 }
61  
62 int main() {
63     int i, X, Y, Z;
64     m = read(), n = read(), s = read();
65     T1 = read(), T2 = read();
66     for (i = 1; i <= m; ++i){
67         X = read(), Y = read(), Z = read();
68         Add_Edges(X, Y, Z);
69     }
70     spfa(T1);
71     ans = dis[s] + dis[T2];
72     spfa(T2);
73     printf("%d\n", min(ans, dis[s] + dis[T1]));
74     return 0;
75 }
View Code(spfa)

相关文章:

  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-10-29
  • 2021-06-13
  • 2022-02-09
  • 2021-08-11
猜你喜欢
  • 2022-12-23
  • 2022-02-22
  • 2022-02-14
  • 2021-07-22
  • 2022-01-13
  • 2021-12-22
  • 2021-08-18
相关资源
相似解决方案