• loj116 有源汇有上下界最大流


    link

    题意&题解

    code:

     1 #include<bits/stdc++.h>
     2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
     3 #define ll long long
     4 #define inf 1000000001
     5 #define y1 y1___
     6 using namespace std;
     7 char gc(){
     8     static char buf[100000],*p1=buf,*p2=buf;
     9     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    10 }
    11 #define gc getchar
    12 ll read(){
    13     char ch=gc();ll x=0;int op=1;
    14     for (;!isdigit(ch);ch=gc()) if (ch=='-') op=-1;
    15     for (;isdigit(ch);ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
    16     return x*op;
    17 }
    18 #define N 205
    19 #define M 9999+N<<1
    20 int n,m,cnt=1,s_,t_,s,t,head[N],d[N],vis[N],cur[N];
    21 struct edge{int to,nxt,c;}e[M];
    22 void adde(int x,int y,int c){
    23     e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;
    24     e[cnt].c=c;//d:原图下界;c:新图容量
    25 }
    26 void ins(int x,int y,int z){
    27     adde(x,y,z);adde(y,x,0);
    28 }
    29 bool bfs(){
    30     queue<int> q;q.push(s);
    31     rep (i,1,t) vis[i]=-1;vis[s]=1;
    32     while (!q.empty()){
    33         int u=q.front();q.pop();
    34         for (int i=head[u];i;i=e[i].nxt){
    35             int v=e[i].to;
    36             if (e[i].c&&vis[v]==-1) vis[v]=vis[u]+1,q.push(v);
    37         }
    38     }
    39     return vis[t]!=-1;
    40 }
    41 int dfs(int u,int flow){
    42     if (u==t) return flow;
    43     int w,used=0;
    44     for (int &i=cur[u];i;i=e[i].nxt){
    45         int v=e[i].to;
    46         if (e[i].c&&vis[v]==vis[u]+1){
    47             w=dfs(v,min(flow-used,e[i].c));
    48             e[i].c-=w,e[i^1].c+=w,used+=w;
    49             if (used==flow) return used;
    50         }
    51     }
    52     if (!used) vis[u]=-1;
    53     return used;
    54 }
    55 int dinic(){
    56     int ret=0;
    57     while (bfs()){
    58         memcpy(cur,head,sizeof(cur));//当前弧优化
    59         ret+=dfs(s,inf);
    60     }
    61     return ret;
    62 }
    63 void del(int u){
    64     for (int &i=head[u];i&&e[i].to>n;i=e[i].nxt);
    65 }
    66 int main(){
    67     n=read(),m=read(),s_=read(),t_=read();
    68     rep (i,1,m){
    69         int x=read(),y=read(),a=read(),b=read();
    70         ins(x,y,b-a);d[x]-=a,d[y]+=a;
    71     }
    72     s=n+1,t=n+2;
    73     rep (i,1,n) if (d[i]>0) ins(s,i,d[i]);else ins(i,t,-d[i]);
    74     ins(t_,s_,inf);//转化为无源汇问题
    75     dinic();
    76     for (int i=head[s];i;i=e[i].nxt) if (vis[e[i].to]!=-1) return puts("please go home to sleep"),0;//判无解
    77     head[s]=head[t]=0;
    78     rep (i,1,n) del(i);//删去i连向t的边
    79     s=s_,t=t_;
    80     printf("%d
    ",dinic());
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    《网络对抗技术》exp7 网络欺诈防范
    《网络对抗技术》exp6 MSF基础应用
    《网络对抗技术》exp5 信息搜集与漏洞扫描
    《网络对抗技术》exp4 恶意代码分析
    《网络对抗技术》Exp3 免杀原理与实践
    《网络对抗技术》exp2 简单后门
    k8s弹性伸缩
    python常见算法
    JavaScript 中创建对象的方法(读书笔记思维导图)
    JavaScript 中的闭包和作用域链(读书笔记)
  • 原文地址:https://www.cnblogs.com/bestFy/p/9320977.html
Copyright © 2020-2023  润新知