# 有上下界的网络流总结（未完成）

LOJ上有全套的模板

  1 # include<iostream>
2 # include<cstdio>
3 # include<cmath>
4 # include<algorithm>
5 # include<cstring>
6 # include<queue>
7 using namespace std;
8 const int mn = 205;
9 const int inf = 0x3f3f3f3f;
10 struct edge{int to,next,flow,cup;};
11 edge e[10200*10];
13 void add(int x,int y,int z)
14 {
15     //printf("%d %d\n",x,y);
16     e[++edge_max].to=y;
17     e[edge_max].flow=e[edge_max].cup=z;
20 }
21 int n,m;
22 int B[mn],low[10200*10];
23 int cur[mn],deep[mn];
24 queue<int> q;
25 bool bfs(int x,int y)
26 {
27     memset(deep,0,sizeof(deep));
28     q.push(x);
29     deep[x]=1;
30     while(!q.empty())
31     {
32         int u=q.front();
33         q.pop();
35         {
36             if(e[i].flow>0 && deep[e[i].to]==0)
37             {
38                 deep[e[i].to]=deep[u]+1;
39                 q.push(e[i].to);
40             }
41         }
42     }
43     /*for(int i=1;i<=y;i++)
44         printf("%d ",deep[i]);
45     printf("\n");*/
46     return deep[y]!=0;
47 }
48 int dfs(int x,int dist,int y)
49 {
50     if(x==y)
51         return dist;
52     for(int &i=cur[x];i;i=e[i].next)
53     {
54         if(e[i].flow>0 && deep[e[i].to]==deep[x]+1)
55         {
56             int di=dfs(e[i].to,min(e[i].flow,dist),y);
57             if(di>0)
58             {
59                 e[i].flow-=di;
60                 e[i^1].flow+=di;
61                 return di;
62             }
63         }
64     }
65     return 0;
66 }
67 int dinic(int x,int y)
68 {
69     int ret=0;
70     while(bfs(x,y))
71     {
72         //memset(cur,0,sizeof(cur));
73         for(int i=1;i<=y;i++)
75         while(int k=dfs(x,inf,y))
76         {
77             //printf("%d\n",k);
78             ret+=k;
79         }
80         //printf("%d\n",ret);
81     }
82     return ret;
83 }
84 int main()
85 {
86     int x,y,a,b;
87     scanf("%d%d",&n,&m);
88     for(int i=1;i<=m;i++)
89     {
90         scanf("%d%d%d%d",&x,&y,&a,&b);
92         B[x]+=a;
93         B[y]-=a;
94         low[i]=a;
95     }
96     int sum=0,st=n+1,en=n+2;
97     for(int i=1;i<=n;i++)
98     {
99         if(B[i]>0){
100             sum+=B[i];
103         }
104         else if(B[i]<0){
107         }
108     }
109     if(dinic(st,en)==sum)
110     {
111         printf("YES\n");
112         for(int i=1;i<=m;i++)
113             printf("%d\n",e[i*2].cup-e[i*2].flow+low[i]);
114     }
115     else printf("NO");
116     return 0;
117 }
View Code

有原有汇最大流

# include<iostream>
# include<cstdio>
# include<algorithm>
# include<cstring>
# include<cmath>
# include<queue>
using namespace std;
const int mn = 220;
const int inf = 0x3f3f3f3f;
struct edge{int to,next,flow,cup;};
edge e[100005];
void add(int x,int y,int z)
{
//printf("%d %d %d\n",x,y,z);
e[++edge_max].to=y;
e[edge_max].flow=e[edge_max].cup=z;
}
int n,m,s,t;
int dfn[mn],cur[mn],deep[mn];
queue<int> q;
bool bfs(int x,int y)
{
memset(deep,0,sizeof(deep));
q.push(x);
deep[x]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
{
if(e[i].flow>0 && deep[e[i].to]==0)
{
deep[e[i].to]=deep[u]+1;
q.push(e[i].to);
}
}
}
return deep[y]!=0;
}
int dfs(int x,int dist,int y)
{
if(x==y) return dist;
for(int &i=cur[x];i;i=e[i].next)
{
if(deep[e[i].to]==deep[x]+1 && e[i].flow!=0)
{
int di=dfs(e[i].to,min(dist,e[i].flow),y);
if(di>0)
{
e[i].flow-=di;
e[i^1].flow+=di;
return di;
}
}
}
return 0;
}
int dinic(int x,int y)
{
int ret=0;
while(bfs(x,y))
{
for(int i=1;i<=n+2;i++)
while(int k=dfs(x,inf,y))
ret+=k;
//printf("%d\n",ret);
}
//printf("%d\n",ret);
return ret;
}
int main()
{
int x,y,a,b;
scanf("%d%d%d%d",&n,&m,&s,&t);
int st=n+1,en=n+2;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&a,&b);
dfn[x]+=a,dfn[y]-=a;
}
/*for(int i=1;i<=n;i++)
printf("%d ",dfn[i]);
printf("\n");*/
for(int i=1;i<=n;i++)
{
if(dfn[i]>0)
{
}
else if(dfn[i]<0)
{
}
}
int tmp=edge_max-1;
dinic(st,en);
{
if(e[i].flow!=0){
//printf("%d %d\n",e[i].to,e[i].flow);
printf("please go home to sleep");
return 0;
}
}
int ans=dinic(s,t);
//printf("%d\n",ans);
ans+=e[tmp].cup-e[tmp].flow;
printf("%d\n",ans);
return 0;
}

有源有汇最小流

# include<iostream>
# include<cstdio>
# include<cmath>
# include<cstring>
# include<algorithm>
# include<queue>
using namespace std;
typedef long long LL;
const int mn = 50010;
const LL inf = 0x3f3f3f3f3f3f3f;
struct edge{
int to,next;
LL flow,cup;
};
edge e[125003*10];
void add(int x,int y,LL z)
{
e[++edge_max].to=y;
e[edge_max].flow=e[edge_max].cup=z;
}
int n,m,s,t;
LL dfn[mn];
int deep[mn],cur[mn];
queue<int> q;
bool bfs(int x,int y)
{
memset(deep,0,sizeof(deep));
q.push(x);
deep[x]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
{
if(deep[e[i].to]==0 && e[i].flow>0)
{
deep[e[i].to]=deep[u]+1;
q.push(e[i].to);
}
}
}
return deep[y]!=0;
}
LL dfs(int x,LL dist,int y)
{
if(x==y) return dist;
for(int &i=cur[x];i;i=e[i].next)
{
if(deep[e[i].to]==deep[x]+1 && e[i].flow!=0)
{
LL di=dfs(e[i].to,min(dist,e[i].flow),y);
if(di>0)
{
e[i].flow-=di;
e[i^1].flow+=di;
return di;
}
}
}
return 0;
}
LL dinic(int x,int y)
{
LL ret=0;
while(bfs(x,y))
{
for(int i=1;i<=n+2;i++)
while(LL k=dfs(x,inf,y))
ret+=k;
}
return ret;
}
int main()
{
int x,y;
LL a,b;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld%lld",&x,&y,&a,&b);
dfn[x]+=a,dfn[y]-=a;
}
int st=n+1,en=n+2;
LL sum=0;
for(int i=1;i<=n;i++)
{
if(dfn[i]>0)
{
sum+=dfn[i];
}
else if(dfn[i]<0)
{
}
}
int tmp=edge_max-1;
if(sum!=dinic(st,en))
{
printf("please go home to sleep");
return 0;
}
else {
LL ans=e[tmp].cup-e[tmp].flow;
}