[USACO5.4]奶牛的电信Telecowmunication

为一条从uv 的,流量为cap 的,费用为cost 的弧与反弧
拆点,对于点x,分为xx+n 两点,x 负责流入,x+n 负责流出
对于每个点连边
对于原图每条连接u,v 的边,连边
跑一边s+nt 的最大流
最大流最小割

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<cstdio>
#include<queue>
using namespace std;
const int M=10000,N=500,INF=1<<25;
int n,m,st,ed,head[N],cnt=0,d[N];
struct edge{int to,next,flow,cap;} e[M];
queue<int> Q;
inline int read()
{
register int x=0,t=1;
register char ch=getchar();
while (ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if (ch=='-') t=-1,ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
void add(int u,int v,int cap)
{
e[cnt].next=head[u];
head[u]=cnt;
e[cnt].to=v;
e[cnt++].cap=cap;
}
int bfs(int s,int t)
{
while (!Q.empty()) Q.pop();
for(int i=1;i<=(n<<1);i++) d[i]=0;
d[s]=1,Q.push(s);
while (!Q.empty()&&!d[t])
{
int x=Q.front();Q.pop();
for(int i=head[x];~i;i=e[i].next)
{
int to=e[i].to;
if (!d[to]&&e[i].flow<e[i].cap)
{
d[to]=d[x]+1;
Q.push(to);
}
}
}
return d[t];
}
int dfs(int x,int t,int flow)
{
if (!flow||x==t) return flow;
int ret=0,new_flow;
for(int i=head[x];~i&&flow;i=e[i].next)
{
int to=e[i].to;
if (d[x]+1==d[to])
{
new_flow=dfs(to,t,min(flow,e[i].cap-e[i].flow));
e[i].flow+=new_flow;
e[i^1].flow-=new_flow;
ret+=new_flow;
flow-=new_flow;
}
}
return ret;
}
int Dinic(int s,int t)
{
int ret=0;
while (bfs(s,t))
ret+=dfs(s,t,INF);
return ret;
}
int main()
{
n=read(),m=read();
st=read(),ed=read();
for(int i=1;i<=n;i++) head[i]=-1;
for(int i=1;i<=n;i++)
{
add(i,i+n,1);
add(i+n,i,0);
}
for(int i=1;i<=m;i++)
{
int u=read(),v=read();
add(u+n,v,INF);
add(v,u+n,0);
add(v+n,u,INF);
add(u,v+n,0);
}
printf("%d",Dinic(st+n,ed));
return 0;
}