Description
Input
当N为0时输入结束。
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
此题和HDU1233基本上一样。先按公路的权值排序后,并查集处理即可。已建成的公路就将其价格置0#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 108
int father[maxn];
struct Edge
{
int u,v,value;
}edge[maxn*(maxn-1)];
int find(int x)
{
if(x==father[x])
{
return x;
}
return find(father[x]);
}
bool cmp(Edge a,Edge b)
{
return a.value>=b.value?0:1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
int mincost=0;
for(int i=1;i<=n;i++)
{
father[i]=i;
}
int k=n*(n-1)/2;
int state;
for(int i=1;i<=k;i++)
{
scanf("%d%d%d%d",&edge[i].u,&edge[i].v,&edge[i].value,&state);
if(state)
{
edge[i].value=0;
}
}
sort(edge+1,edge+k+1,cmp);
for(int i=1;i<=k;i++)
{
if(find(edge[i].u)!=find(edge[i].v))
{
mincost+=edge[i].value;
father[find(edge[i].v)]=edge[i].u;
}
}
cout<<mincost<<endl;
}
return 0;
}
351



被折叠的 条评论
为什么被折叠?



