zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    修改代码的艺术阅读笔记-01
    周总结
    代码整洁之道阅读笔记-03
    周总结
    mybatis三种执行器性能比较
    Tomcat长连接是如何实现的
    Zookeeper在Windows下搭建集群教程
    Zookeeper单机模式下RequestProcessor流程与源码理解
    JDK NIO基础概念与原理
    zookeeper客户端访问服务端时,基于NIO的线程池绑定
  • 原文地址:https://www.cnblogs.com/bestFy/p/9320977.html
Copyright © 2011-2022 走看看