294人阅读 评论(0)

# 继续畅通工程

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18456    Accepted Submission(s): 7979

Problem Description

Input

Output

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

Sample Output
3 1 0

Author
ZJU

Source

Recommend
We have carefully selected several similar problems for you:  1863 1874 1301 1162 1198

#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,j,k,s,x,y,z,ans;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)f[i]=i;
for(s=0,i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
scanf("%d%d%d%d",&x,&y,&z,&k);
if(k==1)f[find(x)]=find(y);
else e[++s].u=x,e[s].v=y,e[s].w=z;
}
sort(e+1,e+s+1,cmp_e);
for(ans=0,i=1;i<=s;i++)
{
x=find(e[i].u),y=find(e[i].v);
if(x!=y)ans+=e[i].w,f[find(x)]=find(y);
}
printf("%d\n",ans);
}
return 0;
}

个人资料
等级：
访问量： 29万+
积分： 6524
排名： 4475
最新评论