B - Network

B - Network

这个题目真坑,用G++居然TLE,用c++ 直接A了,还好在poj直接过了,否则,找错误不得找死啊!!!!

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=15001;
struct node 
{
	int x,y,value;
}edge[maxn];
int fa[maxn];
int find(int x)
{
	return x==fa[x]?x:find(fa[x]); 
}

bool cmp(const node a,const node b)
{
	return a.value<b.value;
}
 
int main()
{
	int m,n;
	int i;
		for(int z=0;z<maxn;z++)
			fa[z]=z;
	while(cin>>m>>n)
	{
	
		for(int z=0;z<n;z++)
		{
			cin>>edge[z].x>>edge[z].y>>edge[z].value;
		}
		sort(edge,edge+n,cmp);
		int num=0,sum=0;
		for(i=0;i<n;i++)
		{
			int x1=find(edge[i].x);
			int x2=find(edge[i].y);
			if(x1!=x2)
			{
				fa[x1]=x2;
				sum+=edge[i].value;
				num++;
			//cout<<edge[i].x<<" "<<edge[i].y<<endl;
			}
			if(num==m-1)
				break;
		}
		cout<<edge[i].value<<endl;
		cout<<i+1<<endl;
		for(int j=0;j<=i;j++)
			cout<<edge[j].x<<" "<<edge[j].y<<endl;
	}
} 


posted @ 2013-04-19 15:23  bo_jwolf  阅读(151)  评论(0编辑  收藏  举报