342人阅读 评论(0)

# 还是畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33978    Accepted Submission(s): 15367

Problem Description

Input

Output

Sample Input
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

Sample Output
3
5

HintHint
Huge input, scanf is recommended.

Source

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1875 1879 1301 1162 1863

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn=1e2;
int f[maxn+10];
struct edge{
int u,v,w;
}e[maxn*maxn+10];

bool cmp_e(edge a,edge b)
{
return a.w<b.w;
}

int find(int x)
{
if(f[x]==x)return x;
return f[x]=find(f[x]);
}

int main()
{
//freopen("1.in","r",stdin);

int n,i,x,y,ans;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)f[i]=i;
for(n=n*(n-1)/2,i=1;i<=n;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
sort(e+1,e+n+1,cmp_e);
for(ans=0,i=1;i<=n;i++)
{
x=find(e[i].u),y=find(e[i].v);
if(x!=y)f[x]=y,ans+=e[i].w;
}
printf("%d\n",ans);
}
return 0;
}

0
0

个人资料
• 访问：287478次
• 积分：6444
• 等级：
• 排名：第4465名
• 原创：353篇
• 转载：59篇
• 译文：0篇
• 评论：31条
最新评论