loj117 有源汇有上下界最小流
Posted bestfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了loj117 有源汇有上下界最小流相关的知识,希望对你有一定的参考价值。
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 60005 19 #define M 130005+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; 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 int main(){ 64 n=read(),m=read(),s_=read(),t_=read(); 65 rep (i,1,m){ 66 int x=read(),y=read(),a=read(),b=read(); 67 ins(x,y,b-a);d[x]-=a,d[y]+=a; 68 } 69 s=n+1,t=n+2; 70 rep (i,1,n) if (d[i]>0) ins(s,i,d[i]);else ins(i,t,-d[i]); 71 dinic();//不加边直接跑,尽可能跑完 72 ins(t_,s_,inf); 73 dinic();//加边以后再跑 74 for (int i=head[s];i;i=e[i].nxt) if (vis[e[i].to]!=-1) return puts("please go home to sleep"),0;//判无解 75 printf("%d ",e[cnt].c); 76 return 0; 77 }
以上是关于loj117 有源汇有上下界最小流的主要内容,如果未能解决你的问题,请参考以下文章