zoukankan      html  css  js  c++  java
  • 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
    ",ans-maxflow);
        return 0;
    }
  • 相关阅读:
    C#读写XML no
    ComboBox、ListBox绑定和获取数据 no
    win7 安装oracle 11g no
    2011没有惊天动地 no
    Oracle 删除重复数据只留一条 no
    Oracle 创建用户并分配权限 no
    Oracle 导出、导入某用户所有数据(包括表、视图、存储过程...) no
    谢谢,博客园这么快就审批通过了我的申请!
    Eclipse 中 JAVA AWT相关包不提示问题(解决)
    Eclipse java项目转Maven项目
  • 原文地址:https://www.cnblogs.com/hua-dong/p/10038417.html
Copyright © 2011-2022 走看看