您的位置:首页 > 其它

POJ 1861 Network Krusakl模板题 最小生成树

2015-10-22 20:35 465 查看
!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!

简直污

Sample Output
1
4
1 2
1 3
2 3
3 4

这是错的
正确应该是

Sample Output
1
3
1 2
1 3
3 4

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 15010
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
struct Edge{
int u,v,w;
Edge(){}
Edge(int _u,int _v,int _w):u(_u),v(_v),w(_w){}
friend bool operator<(const Edge a,const Edge b){
return a.w<b.w;
}
}my[maxn];
int fa[maxn],n,m;
int find_fa(int x){
return x==fa[x]?x:(fa[x]=find_fa(fa[x]));
}
void krusakl(){
int aa[maxn],bb[maxn];
sort(my,my+m);
int x,y,maxx=0;
int cnt=0;///记录加入的边
FOR(i,0,m-1){
x=find_fa(my[i].u);
y=find_fa(my[i].v);
if(x!=y){
fa[y]=x;
aa[cnt]=my[i].u;
bb[cnt]=my[i].v;
cnt++;
maxx=maxx>my[i].w?maxx:my[i].w;
}
if(cnt>=n-1)break;
}
printf("%d\n%d\n",maxx,n-1);
FOR(i,0,cnt-1)printf("%d %d\n",aa[i],bb[i]);
}
int main(){
while(rd2(n,m)!=EOF){
int u,v,w;
FOR(i,0,m)fa[i]=i;
FOR(i,0,m-1){
rd2(u,v);
rd(w);
my[i]=Edge(u,v,w);
}
krusakl();
}
return 0;
}
/*
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: