洛谷 P1396

    科技2026-02-12  2

    题目链接

    题目大意

    有一个城市,有n个城镇,m条道路,每条道路都有一个拥挤值,现在你要从s城镇走到t城镇,求出从s走到t所遇到的拥挤值的最大值的最小值。

    思路

    这道题很像一棵最小生成树,但与最小生成树不同的是,他不是所有的点都需要用上,也就是说有一些城镇是不需要经过的,那我们只要在城镇s和城镇t第一次联通的时候,输出那条使s城镇和t城镇构成联通的边就好了。

    上代码!!!

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<map> #define ll long long #define pi acos(-1) #define inf 0x3f3f3f3f #define pii pair<int, int> #define fi first #define se second #define mp(a, b) make_pair(a, b) #define piii pair<pii, int> #define uf(a, b, i) for (register int i = (a); i <= (b); ++i) #define df(a, b, i) for (register int i = (a); i >= (b); --i) using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template<class T> inline void print(T x) { if(x > 9) print(x/10); putchar(x%10 + '0'); } template<class T> T Max(T a, T b) { return a > b ? a : b; } template<class T> T Min(T a, T b) { return a < b ? a : b; } const ll mod = 1e9 + 7; int n, m, s, t; int f[10004]; struct edg { int u, v, w; bool operator < (const edg &ee) { return w < ee.w; } } e[20004]; int find(int x) { return f[x] == x ? x : (f[x] = find(f[x])); } void scan() { n = read(); m = read(); s = read(); t = read(); uf (1, m, i) { e[i].u = read(); e[i].v = read(); e[i].w = read(); } } void work() { sort(e+1, e+m+1); uf (1, n, i) f[i] = i; uf (1, m, i) { int fu = find(e[i].u), fv = find(e[i].v); if (fu != fv) { f[fu] = fv; } else continue; if (find(s) == find(t)) { print(e[i].w); putchar('\n'); return ; } } } int main() { scan(); work(); return 0; }
    Processed: 0.022, SQL: 9