Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

样例明显错了,生成树四个顶点,怎么会还有四条边

Posted by 15211160230 at 2016-08-24 09:55:51 on Problem 1861
顺便再贴个代码
#include <stdio.h>
#include <algorithm> 
#define MAXN 1001
int vexnum,edgenum,parent[MAXN];
using namespace std;
typedef struct
{	int s,e,c;
}Edge;
Edge de[15001];
bool cmp(Edge i,Edge j)
{	if(i.c<j.c)	return true;
	return false;
}
void Initial(int N)
{	int i,j;
	sort(de,de+edgenum,cmp);
	for(i=1;i<=N;++i)
		parent[i]=i;
}
int FindRoot(int x)
{	if(x!=parent[x])
		parent[x]=FindRoot(parent[x]);
	return parent[x];
}
bool Union(int i,int j)
{	int x=FindRoot(i),y=FindRoot(j);
	if(x==y)	return false; 
	parent[x]=y;
	return true;
}
void kruskal()
{	int i,k;
	int s[MAXN],num=0;
	for(i=0;i<edgenum;++i)
	{	if(Union(de[i].s,de[i].e)==true)
			s[num++]=i;
		if(num>vexnum-1)	break;
	}
	printf("%d\n",de[s[num-1]].c);
	printf("%d\n",num);
	for(i=0;i<num;++i)
		printf("%d %d\n",de[s[i]].s,de[s[i]].e);
}
int main()
{	int N,i,M;
	int a,b,c;
	scanf("%d %d",&N,&M);
	vexnum=N,edgenum=M;
	for(i=0;i<M;++i)
		scanf("%d %d %d",&de[i].s,&de[i].e,&de[i].c);
	Initial(N);
	kruskal();
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator