首页 > 代码库 > HDU-4647 Another Graph Game 贪心

HDU-4647 Another Graph Game 贪心

将边权拆成两半加到它所关联的两个点的点权中即可。因为当两个人分别选择不同的点时,这一权值将互相抵消。然后排序从最优开始取。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <vector>
#define LL long long
using namespace std;
const int maxn=110000;
bool cmp(LL a, LL b)
{
	return a>b;
}
LL n,m;
LL w[maxn];
int main()
{
	LL u,v,l;
	while(scanf("%I64d%I64d",&n,&m)!=EOF)
	{
		for(LL i=1;i<=n;i++)
		{
			scanf("%I64d",&w[i]);
			w[i]*=2ll;
		}
		for(LL i=0;i<m;i++)
		{
			scanf("%I64d%I64d%I64d",&u,&v,&l);
			w[u]+=l;
			w[v]+=l;
		}
		sort(w+1,w+1+n,cmp);
		LL k=1;
		LL s1=0ll;
		LL s2=0ll;
		while(k<=n)
		{
			if(k%2==1)
			{
				s1+=w[k];
			}
			else
			{
				s2+=w[k];
			}
			k++;
		}
		LL ans=s1-s2;
		printf("%I64d\n",ans/2);
	}
	return 0;
}


HDU-4647 Another Graph Game 贪心