BZOJ 2563 raccoon and peach game greedy, bzoj2563
Given an undirected graph, each vertex has a certain weight, and each edge has an edge weight. Two people choose points in turn. If the two ends of an edge are selected, the edge is selected, both of them want their own scores-the opponent has the largest score, and the final first hand score-the latter hand score
Consider the impact of first-hand selection of each point on the answer
If you do not select a point, the contribution to the answer is-w.
If you select a point, the contribution to the answer is w.
If neither of the two endpoints is selected, the contribution to the answer is-c.
If only one of the two endpoints is selected, the contribution to the answer is 0.
If both endpoints are selected, the contribution to the answer is c.
Then we first subtract all the weights from the initial answer in advance, and then it becomes:
If you do not select a point, the contribution to the answer is 0.
If you select a point, the contribution to the answer is.
If neither of the two ends of an edge is selected, the contribution to the answer is 0.
If only one of the two endpoints is selected, the contribution to the answer is c.
If both endpoints are selected, the contribution to the answer is 2 * c
So that the contribution of a vertex is twice the vertex weight + the edge weight of all connected Edges
Sort the order to take the maximum order.
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 10100using namespace std;int n,m;long long ans,a[M];int main(){int i,x,y,z;cin>>n>>m;for(i=1;i<=n;i++){scanf("%d",&z);ans-=z;a[i]=z<<1;}for(i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&z);ans-=z;a[x]+=z;a[y]+=z;}sort(a+1,a+n+1);for(i=2;i<=n;i+=2)ans+=a[i];cout<<ans<<endl;return 0;}