首页 > 其他分享 > Educational Codeforces Round 55 (Rated for Div. 2) G. Petya and Graph 网络流|

Educational Codeforces Round 55 (Rated for Div. 2) G. Petya and Graph 网络流|

时间:2023-03-02 11:01:38浏览次数:36  
标签:dep Educational Rated 55 ll cnt int add maxn

很经典,想记录一下

网络流里有一个很典的trick,求最大获利转化成最小损失

求最小损失转化成割边

求的是max(边权和-边所连接的点权和),考虑把边看成左部点,把点看成右部点

刚开始我们假设边全都要选,那么最小割也就是选取左部所有点,去掉右边所有点

也就是把所有边都选了,再扣掉所有点(根据最小割的意义)

然后考虑不要某些项目,我们可以割s和该项目连的边,表示不要这个获利

那跑个最小割,输出时用sigma(wi)-最小割即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50000;
const ll inf=LONG_LONG_MAX;
struct lys{
    ll from,to,nxt,c;
}e[maxn*4];
ll cnt=1,n,m,s,t,dep[maxn],head[maxn],val[maxn],cur[maxn];
void add(int from,int to,ll c)
{
    cnt++;
    e[cnt].from=from;e[cnt].to=to;e[cnt].nxt=head[from];head[from]=cnt;val[cnt]=c;
}

bool  bfs()
{   queue<int>q;
    memset(dep,0,sizeof(dep));
    q.push(s);
    dep[s]=1;
    cur[s]=head[s];
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=e[i].nxt)
        {
            int to=e[i].to;
            if(val[i]&&!dep[to])
            {
                dep[to]=dep[u]+1;
                cur[to]=head[to];
                q.push(to);
            }
        }
    }
    return dep[t];    
}
ll dfs(int u,ll in)
{
    if(u==t) 
    return in;
    ll out=0;
    for(int i=cur[u];i&&in;i=e[i].nxt)
    {
      cur[u]=i;
      int to=e[i].to;
      if(val[i]&&dep[to]==dep[u]+1)
      {
          ll res=dfs(to,min(val[i],in));
          val[i]-=res;
          val[i^1]+=res;
          in-=res;
          out+=res;
      }    
    }
    
    if(!out) 
      dep[u]=0;
    return out;
}
int a[maxn];
int main()
{  // freopen("lys.in","r",stdin);

    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    s=0;
    t=n+m+1;
    ll sum=0;
    for(int i=1;i<=m;i++){
        int u,v;ll w;cin>>u>>v>>w;
        sum+=w;
        u+=m;v+=m;
        add(i,u,inf);
        add(u,i,0);
        add(i,v,inf);
        add(v,i,0);
        add(s,i,w);
        add(i,s,0); 
    }
    for(int i=1;i<=n;i++) add(i+m,t,a[i]),add(t,i+m,0);
    ll ans=0;
    while(bfs())     ans+=dfs(s,1e18);
    cout<<sum-ans;
}

 

标签:dep,Educational,Rated,55,ll,cnt,int,add,maxn
From: https://www.cnblogs.com/liyishui2003/p/17171050.html

相关文章

  • Educational Codeforces Round 144 (Rated for Div. 2)
    链接EducationalCodeforcesRound144(RatedforDiv.2)只会两个题太弱了A题先打表找出一个很长的字符字串然后,用strstr查找找到yes找不到no#include<iostream>......
  • educational round 1796 D & E 解题报告
    Part1D【题意】有一个数组\(\{a_n\}\),给定\(x,k\),现在要将数组内\(k\)个数加上\(x\),其他的数减去\(x\)。问得到的所有可能数组中最大子段和的最大值。\(1\len......
  • CFR-755-Div-2解题报告
    比赛传送门赛时AC三道,补题做出一道。A.MathematicalAddition{%noteinfono-iconProblem%}给你两个正整数\(u,v\),求一对合法的\(x,y\)使得\(\frac{x}{u}+\fra......
  • Educational Codeforces Round 143 (Rated for Div
    EducationalCodeforcesRound143(RatedforDiv.2)Problem-BIdealPoint给定n个线段区间\([l,r]\),我们定义\(f(x)\)为覆盖点\(x\)的线段数,我们每次操作可以删......
  • LeetCode算法训练-贪心算法 455.分发饼干 376. 摆动序列 53. 最大子序和
    欢迎关注个人公众号:爱喝可可牛奶LeetCode算法训练-贪心算法455.分发饼干376.摆动序列53.最大子序和前置知识贪心算法核心是找局部最优解,通过局部最优推导出全局最......
  • Educational Codeforces Round 143 (Rated for Div. 2)(A,C,D)
    EducationalCodeforcesRound143(RatedforDiv.2)(A,C,D)好久没有写题了,这次\(vp\)竟然连\(vs\)都不会用了,O(∩_∩)OAA这个也是差一点了,还有一个情况我的解法是没有......
  • Educational Codeforces Round 26
    EducationalCodeforcesRound26https://codeforces.com/contest/837E公式推导看明白了,但是因子求解部分的代码还不是很懂,所以还没有补A.TextVolume#include<bits/......
  • Educational Codeforces Round 112 (Rated for Div
    EducationalCodeforcesRound112(RatedforDiv.2)CodeForces-1555DSayNotoPalindromes如果一个字符串中不包含长度2以上的回文子串,我们就说这个字符串是漂亮......
  • 剑指 Offer 55 - II. 平衡二叉树(java解题)
    目录1.题目2.解题思路3.数据类型功能函数总结4.java代码1.题目输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超......
  • [CF755G] PolandBall and Many Other Balls 题解
    [CF755G]PolandBallandManyOtherBalls题解题意概括有一排\(n\)个球,定义一个组可以只包含一个球或者包含两个相邻的球。现在一个球只能分到一个组中,求从这些球中......