营救

原题链接:https://www.luogu.org/problemnew/show/1396#sub

日常打反n和m(1/1)

日常数组开小(1/1)

热身题。虽然说是最大值最小,但实际上不二分也行的。

一遍spfa记录一下路径上的最大dis就好,正确性显然。

当然并查集也是可以做的。

参考代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #define INF 2147483647
 6 #define maxn 888888
 7 using namespace std;
 8 int n,m,s,t;
 9 int ans;
10 struct Edge{
11     int from,to,dis;
12 };
13 Edge edge[maxn];
14 int head[maxn];
15 int dis[maxn];
16 int tot = 0;
17 void add_edge(int from,int to,int dis){
18     edge[++tot].from = head[from];
19     edge[tot].to = to;
20     edge[tot].dis = dis;
21     head[from] = tot;
22 }
23 inline int read(){
24     int num = 0;
25     char c;
26     while ((c = getchar()) == ' ' || c == '
' || c == '
');
27     num = c - '0';
28     while (isdigit(c = getchar()))
29         num = num * 10 + c - '0';
30     return num;
31 }
32 bool inq[maxn];
33 void spfa(){
34     queue<int> q;
35     memset(inq,false,sizeof(inq));
36     for (register int i=1;i<=n;i++)
37         dis[i] = INF;
38     dis[s] = 0;
39     q.push(s);
40     inq[s] = true;
41     while (!q.empty()){
42         int u = q.front();
43         q.pop();
44         inq[u]= false;
45         for (int i = head[u];i;i=edge[i].from){
46             int v = edge[i].to;
47             int w = edge[i].dis;
48             if (max(w,dis[u]) < dis[v]){
49                 dis[v] = max(w,dis[u]);
50                 if (!inq[v]){
51                     inq[v] = true;
52                     q.push(v);
53                 }
54             }
55             
56         }
57     }
58 }
59 int main(){
60     n = read();m = read();s = read();t = read();
61     for (register int i=1;i<=m;i++){
62         int u,v,d;
63         u = read();v = read();d = read();
64         add_edge(u,v,d);
65         add_edge(v,u,d);
66     }
67     spfa();
68     printf("%d",dis[t]);
69     return 0;
70 }
原文地址:https://www.cnblogs.com/OIerShawnZhou/p/7732046.html