洛谷 P1396

题目链接

题目大意

有一个城市,有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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值