Live2d Test Env

CF1082G:G. Petya and Graph(裸的最大闭合权图)

Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of

The weight of the

The weight of the

A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.

The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.

Input

The first line contains two numbers

The next line contains

The following

Output

Print one integer — the maximum weight of the subgraph of the given graph.

Examples
Input
4 5
1 5 2 2
1 3 4
1 4 4
3 4 5
3 2 2
4 2 2
Output
8
Input
3 3
9 7 8
1 2 1
2 3 2
1 3 3
Output
0

题意:让你选一些边,选边的前提是端点都被选了,求最大的边权和-点权和。

思路:这不是BZOJ原题嘛.jpg。比如BZOJ3438:小M的作物BZOJ3894:文理分科。

就是先得到所有的贡献sum,然后减去最小割。因为求最小割的过程,其实是一个取min的过程。

(福利题啊!CF损失50分的感觉

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=400010;
const ll  inf=1e18;
int N,M,S,T,cnt=1; ll ans,maxflow,cap[maxn];
int Laxt[maxn],To[maxn],Next[maxn],vd[maxn],dis[maxn];
void add(int u,int v,ll c)
{
    Next[++cnt]=Laxt[u];Laxt[u]=cnt; To[cnt]=v; cap[cnt]=c;
    Next[++cnt]=Laxt[v];Laxt[v]=cnt; To[cnt]=u; cap[cnt]=0;
}
ll sap(int u,ll flow)
{
    if(flow==0||u==T) return flow;
    ll tmp,delta=0;
    for(int i=Laxt[u];i;i=Next[i]){
        if(dis[u]==dis[To[i]]+1&&cap[i]>0){
           tmp=sap(To[i],min(cap[i],flow-delta));
           delta+=tmp; cap[i]-=tmp; cap[i^1]+=tmp;
           if(delta==flow||dis[S]>T+1) return delta;
        }
    }
    vd[dis[u]]--;
    if(vd[dis[u]]==0) dis[S]=T+2;
    vd[++dis[u]]++;
    return delta;
}
int main()
{
    int u,v,x; ll c; scanf("%d%d",&N,&M);
    S=0; T=N+M+1;
    for(int i=1;i<=N;i++){
        scanf("%d",&x);
        add(i,T,x);
    }
    for(int i=1;i<=M;i++){
         scanf("%d%d%lld",&u,&v,&c); ans+=c;
         add(N+i,u,inf); add(N+i,v,inf);  add(S,N+i,c);
    }
    while(dis[S]<=T) maxflow+=sap(S,inf);
    printf("%lld\n",ans-maxflow);
    return 0;
}

 

posted @ 2018-11-29 15:05  nimphy  阅读(770)  评论(0编辑  收藏  举报