| ||||||||||
| 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 | |||||||||
为什么kruskal WA??#include<algorithm>
using namespace std;
struct edge
{
int x,y,c;
};
edge e[10000];
int t,m,n,s,total,f[30],temp,i;
char ch,ch1;
bool cmp(edge xx,edge yy)
{
return(xx.c<yy.c);
}
int top(int x)
{
if (f[x]!=x)
{
f[x]=top(f[x]);
}
return(f[x]);
}
void union0(int i,int j,int k)
{
int x,y;
x=top(i);
y=top(j);
if (x!=y)
{
total+=k;
s++;
f[x]=y;
}
}
void kruskal()
{
int i;
sort(e+1,e+1+m,cmp);
for (i=1;i<=n;i++)
f[i]=i;
for (i=1;i<=m;i++)
{
union0(e[i].x,e[i].y,e[i].c);
if (s==n-1)break;
}
}
int main()
{
while(scanf("%d",&n)&&n!=0)
{
m=0;
getchar();
for (i=1;i<=n-1;i++)
{
scanf("%c",&ch);
scanf("%d",&t);
getchar();
while (t>0)
{
t--;
scanf("%c %d",&ch1,&temp);
getchar();
m++;
e[m].x=ch-'A'+1;
e[m].y=ch1-'A'+1;
e[m].c=temp;
}
}
s=0;
total=0;
kruskal();
printf("%d\n",total);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator