码迷,mamicode.com
首页 > 其他好文 > 详细

loj117 有源汇有上下界最小流

时间:2018-07-16 23:43:22      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:flow   nic   memcpy   stdin   技术   fread   amp   queue   sleep   

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 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\n",e[cnt].c);
76     return 0;
77 }
View Code

 

loj117 有源汇有上下界最小流

标签:flow   nic   memcpy   stdin   技术   fread   amp   queue   sleep   

原文地址:https://www.cnblogs.com/bestFy/p/9320994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!