| ||||||||||
| 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 | |||||||||
为什么老是RT呀??帮帮忙!!!!!!!!!!!#include<stdio.h>
#include<algorithm>
using namespace std;
struct st
{
int s,e,dis;
}edge[200010];
bool cmp(const st&a,const st&b)
{
return a.dis>b.dis;
}
int flag = 0;
int Kruskal(int v,int e)
{
int i,j,an1,an2,sum = 0;
int count = 1;
int set[100001];
for(i = 1;i <= v;i++)
set[i] = i;
sort(edge + 1,edge + e + 1,cmp);
i = 1;
while(count < v)
{
an1 = set[edge[i].s];
an2 = set[edge[i].e];
if(an1 != an2)
{
count++;
if(edge[i].dis == -1)
{
flag = 1;
break;
}
else
sum += edge[i].dis;
for(j = 1;j <= v;j++)
if(set[j]==an2)
set[j] = an1;
}
// printf("%d\n",sum);
i++;
}
return sum;
}
int main()
{
int i,j,n,m,k,sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
k = 0;
for(i = 1;i <= n;i++)
for(j = 1;j <= n;j++)
{
edge[++k].s = i;
edge[k].e = j;
edge[k].dis = -1;
}
for(i = 1;i <= m;i++)
{
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].dis);
}
sum = Kruskal(n,m);
if(flag)
printf("-1\n");
else
printf("%d\n",sum);
}
return 0;
}
/*Sample Input
5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17
Sample Output
42
*/
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator