poj 2395 MST的最大边(水)
来源:互联网 发布:钻孔加工编程例子 编辑:程序博客网 时间:2024/05/18 22:12
点击打开链接
题意:从源点能到任意一点且 每走1单位长度就要消耗一单位水,每到一个农场可以把自己的水充满,求至少要带的水。
思路:图要是连通的&&每条边尽量小 (若已经连通 在加边的只可能增大ans) -》 求MST的最大边即可
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int M = 51010;struct edge{long u;long v;long long cost;bool operator <(const edge &t)const{return cost<t.cost; }}e[M];long n,p,m,fa[M];void Inin(){for(int i=1;i<n;i++) //{fa[i]=i;}}int find(int x){if(x!=fa[x]){fa[x]=find(fa[x]); //路径压缩}return fa[x];}void Union(int x,int y){int a=find(x);int b=find(y);if(a!=b){fa[a]=b;}}void Kruskal(){Inin();// union-set 初始化 long long ans=0;for(int i=0;i<m;i++){if(find(e[i].u)!=find(e[i].v)){Union(e[i].u,e[i].v);ans=max(ans,e[i].cost);}}cout<<ans<<endl;}int main() {cin>>n>>m;for(int i=0;i<m;i++){scanf("%d%d%ld",&e[i].u,&e[i].v,&e[i].cost);}sort(e,e+m);Kruskal(); //每次将两个连通分量合并成一个时 花费最小 return 0;}
0 0
- poj 2395 MST的最大边(水)
- POJ 2395 Out of Hay【求MST中最长边】
- POJ 2395 Out of Hay (MST)
- POJ 1789 Truck History MST(水)
- POJ 1679 The Unique MST【MST是否唯一,Prime算法,最好的代码】
- POJ 1679 The Unique MST(判断MST的唯一性)
- [MST] poj 1679
- poj 1861 Network MST
- poj 1751 Highways MST
- POJ 3723 Conscription MST
- poj The Unique MST
- poj-1861-MST
- poj 1251 MST
- poj 1287 MST(kruskal)
- poj 3723 MST
- POJ 1861 - MST - Krustral
- POJ 3625 - MST
- POJ 1251 MST Krustral
- iOS开发支付集成之微信支付
- git.oschina免费的、分布式代码管理工具
- UE4通过代码逻辑触发相应事件,相应事件逻辑在蓝图实现
- MYSQL笔记二
- 求5阶矩阵其对角线上所有元素之和
- poj 2395 MST的最大边(水)
- 三个方法让你成为更好的程序员
- android开发中涉及到的度量单位
- HDE 1180 诡异的楼梯
- shell学习-2
- <<More Effective C++>>读书笔记1: 基础议题
- android自定义view三(draw和ondraw)
- 苹果App Store公布最新应用审核标准:更加严格
- Qt定时器两种用法