zju2008继续畅通工程(村庄已建成)

http://acm.hdu.edu.cn/showproblem.php?pid=1879

浙大计算机研究生复试上机考试-2008年


#include <stdio.h>
#include <algorithm>
using namespace std;
struct Edge
{
  int a,b;
  int cost;
  int tag;                    //标志是否已建成,tag=1,则成本就为0,优先考虑
}edge[5000];

int Tree[100];

bool cmp(Edge A,Edge B)              //本题核心在于重写cmp函数
{
   if (A.tag!=B.tag)                 //已建成的排序在前,优先选择该边
   {
	   return A.tag>B.tag;      //也就是tag=1的在前,tag=0时,再按cost排序
   }                         
   else
   {
       return A.cost<B.cost;
   }
}
int findRoot(int x)
{
  if (Tree[x]==-1)
  {
	  return x;
  }
  else
  {
	  int tmp=findRoot(Tree[x]);
	  Tree[x]=tmp;
	  return tmp;
  }
}
int main()
{
  int n,i,a,b;
  while (scanf("%d",&n)!=EOF && n!=0)
  {
	  for(i=1;i<=n*(n-1)/2;i++)
	  {
		  scanf("%d%d%d%d",&edge[i].a,&edge[i].b,&edge[i].cost,&edge[i].tag);
	  }
	  sort(edge+1,edge+1+n*(n-1)/2,cmp);      //所有边排序,别写成村庄数n
	  for (i=1;i<=n;i++)
	  {
		  Tree[i]=-1;
	  }
	  int ans=0;
	  for (i=1;i<=n*(n-1)/2;i++)
	  {
		 a=findRoot(edge[i].a);
		 b=findRoot(edge[i].b);
		 if (a!=b)
		 {
			 Tree[a]=b;
			 if (edge[i].tag==0)
			 {
                ans+=edge[i].cost;
			 }
		 }
	  }
		  printf("%d\n",ans);

  }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值