| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么re了呢?求大牛解答!!!#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int father[2010],flag[2010];
typedef struct node
{
int c1;
int c2;
int d;
}Node;
Node point[2010];
int cmp(const void *_a,const void *_b)
{
Node *a=(Node*)_a;
Node *b=(Node*)_b;
return a->d-b->d;
}
void init(int n)
{
int i;
for(i=1;i<=n;i++)
father[i]=i;
}
int dfs(int x)
{
return x==father[x]?x:dfs(father[x]);
}
int merge(int a,int b)
{
int c1=dfs(a);
int c2=dfs(b);
if(c1==c2)
return 0;
else
{
if(c1>c2)
father[c1]=c2;
else
father[c2]=c1;
return 1;
}
}
int main()
{
int n,m,i,j,sum,count,mlen;
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
scanf("%d %d %d",&point[i].c1,&point[i].c2,&point[i].d);
qsort(point+1,m,sizeof(point[0]),cmp);
init(n);
count=0;
sum=0;
mlen=0;
memset(flag,0,sizeof(flag));
for(i=1;i<=m;i++)
{
if(merge(point[i].c1,point[i].c2))
{
flag[i]=1;
if(point[i].d>mlen)
mlen=point[i].d;
sum+=point[i].d;
count++;
}
if(count==n-1)
break;
}
printf("%d\n",mlen);
printf("%d\n",count);
for(i=1;i<=m;i++)
if(flag[i])
printf("%d %d\n",point[i].c1,point[i].c2);
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator