码迷,mamicode.com
首页 > 其他好文 > 详细

【USACO 2008 Nov Gold】 2.Cheering up the Cows 最小生成树、

时间:2015-01-02 14:44:44      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:usaco 2008 nov gold   cheering up the cows   最小生成树   

题意:可以理解为给你一个图,让你建一个树,然后从某根开始走,每走一条边就要消耗两边点权+边的边权。最后再加上一个根的点权,问最少花多少代价。

题解:改变边权然后做最小生成树。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 101000
#define inf 0x3f3f3f3f
using namespace std;
struct KSD
{
	int u,v,len;
	bool operator < (const KSD &a)const{return len<a.len;}
}road[N];
int p[N];
int n,m,ans=inf;
int f[N];int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
int main()
{
	int i,j,k;
	int a,b,c;
	scanf("%d%d",&n,&m);
	
	for(i=1;i<=n;i++)
	{
		scanf("%d",&p[i]);
		ans=min(ans,p[i]);
		f[i]=i;
	}
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		road[i].u=a,road[i].v=b,road[i].len=p[a]+p[b]+c*2;
	}
	sort(road+1,road+m+1);
	for(i=1;i<=m;i++)
	{
		int fa=find(road[i].u),fb=find(road[i].v);
		if(fa!=fb)
		{
			ans+=road[i].len;
			f[fb]=fa;
		}
	}
	printf("%d\n",ans);
	return 0;
}


【USACO 2008 Nov Gold】 2.Cheering up the Cows 最小生成树、

标签:usaco 2008 nov gold   cheering up the cows   最小生成树   

原文地址:http://blog.csdn.net/vmurder/article/details/42340545

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!