bzoj 3714: [PA2014]Kuglarz【最小生成树】

参考:https://blog.csdn.net/aarongzk/article/details/48883741
没想到吧.jpg
来自题解:
“如果用sum[i]表示前i个杯子底球的总数,那么知道一个c[i][j],等于是知道了sum[j]和sum[i-1]的差的奇偶性。而sum[0]的奇偶性是知道的,所以只需要知道所有sum[i]与sum[0]的差的奇偶性,就可以推出每个杯子是否有球。”
所以这意味着什么呢,你需要直接或间接的知道sum[i]和sum[0]之间的关系,并且,知道sum[i-1]和sum[j]的关系需要支付c[i][j]的代价
眼熟吗
没错!就是最小生成树!满脑子网络流的我可以醒醒了
跑个克鲁斯卡尔啥的就结了

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2005;
int n,f[N],tot,con;
struct qwe
{
	int u,v,w;
}e[N*N];
bool cmp(const qwe &a,const qwe &b)
{
	return a.w<b.w;
}
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
int zhao(int x)
{
	return x==f[x]?x:f[x]=zhao(f[x]);
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)
		for(int j=i;j<=n;j++)
		{
			int x=read();
			e[++tot]=(qwe){i,j+1,x};
		}
	sort(e+1,e+1+tot,cmp);
	for(int i=1;i<=n+1;i++)
		f[i]=i;
	long long ans=0;
	for(int i=1;i<=tot&&con<n;i++)
	{
		int fu=zhao(e[i].u),fv=zhao(e[i].v);
		if(fu!=fv)
		{
			f[fu]=fv;
			con++;
			ans+=e[i].w;
		}
	}
	printf("%lld\n",ans);
	return 0;
}
posted @ 2018-04-26 11:25  lokiii  阅读(118)  评论(0编辑  收藏  举报