题目链接:http://poj.org/problem?id=1797
题意: 给处一个图,n个顶点和m条边,每个边都有最大承载量,现在我要从1点运送货物到n点,求能运送货物的最大重量。
注意: 只需要从 1 到 n , 不需要构建出完全的最大生成树, 故 用coun 计数 加入最大生成树的, 当 coun == n - 1 时才break 会WA. 最后输出的时候要输出两个空行
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int fa[maxn];
int maxx;
struct xx{
int l,r,w;
xx(){}
xx(int l,int r,int w):l(l),r(r),w(w){}
}edge[maxn*100];
int coun = 0;
void init(){
for(int i = 0;i < maxn;i ++)
fa[i] = i;
maxx = 1e9; coun = 0;
}
bool cmp(xx A,xx B){
return A.w > B.w;
}
int _find(int x){
if(fa[x] == x) return x;
return fa[x] = _find(fa[x]);
}
void _union(int x,int y,int w){
int fx = _find(x), fy = _find(y);
if(fx == fy) return ;
maxx = min(w,maxx);
fa[fx] = fy;
coun ++;
}
int main()
{
int n,m; int x = 1;
int t; scanf("%d",&t);
while(t --){
init();
scanf("%d%d",&n,&m);
int a,b,c;
for(int i = 0;i < m;i ++){
scanf("%d%d%d",&a,&b,&c);
edge[i] = xx(a,b,c);
}
sort(edge,edge + m,cmp);
for(int i = 0;i < m;i ++){
_union(edge[i].l,edge[i].r,edge[i].w);
if(_find(1) == _find(n)) break;
}
printf("Scenario #%d:\n",x ++);
printf("%d\n\n",maxx);
}
return 0;
}