USACO Section5.1 Pollutant Control(milk6)

Problem Summary

Given a directed graph, find the minimum cut.

Solution

At first, we need to know the max-flow min-cut theorem :

In optimization theory, the max-flow min-cut theorem states that in a flow network, the maximum amount of flow passing from the source to the sink is equal to the total weight of the edges in the minimum cut, i.e. the smallest total weight of the edges which if removed would disconnect the source from the sink.

Then we can use Dinic’s algorithm to compute the max flow from the source to the sink, in this case, i.e. from 1 to n.

As for which edges are in the minimum cut, I use DFS to generate all possible combinations(whose total weight is no more than the max flow), and for each combination, check if it is a cut.

Code

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
using namespace std;
const int maxn = 40;
const int maxm = 1002;
int n,m,max_flow;
int cost[maxn][maxn],g[maxn][maxn];
int st[maxm],en[maxm],c[maxm];
int head,tail,qu[maxn],level[maxn];
int idx[maxm],min_num;
bool now[maxm],best[maxm];
bool bfs()
{
memset(level,0,sizeof(level));
head=0; tail=1;
qu[0]=1;
level[1]=1;
while(head<tail)
{
int cur=qu[head];
head++;
for(int i=1;i<=n;i++)
if(level[i]==0 && cost[cur][i]>0)
{
level[i]=level[cur]+1;
qu[tail]=i;
tail++;
}
}
return (level[n]!=0);
}
int flow(int cur,int max_flow)
{
if(cur==n) return max_flow;
for(int i=1;i<=n;i++)
if(level[i]==level[cur]+1 && cost[cur][i]>0)
{
int tmp=flow(i,min(max_flow,cost[cur][i]));
if(tmp<=0) continue;
cost[cur][i]-=tmp;
cost[i][cur]+=tmp;
return tmp;
}
return 0;
}
int dinic()
{
int ans=0;
while(bfs())
ans+=flow(1,INT_MAX);
return ans;
}
void get_best(int cur,int sumcost,int sumn)
{
if(sumn>=min_num) return;
if(cur>idx[0])
{
if(sumcost!=max_flow) return;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cost[i][j]=g[i][j];
for(int i=1;i<=idx[0];i++)
if(now[i])
cost[st[idx[i]]][en[idx[i]]]-=c[idx[i]];
if(!bfs())
{
min_num=sumn;
for(int i=1;i<=idx[0];i++) best[i]=now[i];
}
return;
}
if(sumcost+c[idx[cur]]<=max_flow)
{
now[cur]=true;
get_best(cur+1,sumcost+c[idx[cur]],sumn+1);
}
now[cur]=false;
get_best(cur+1,sumcost,sumn);
}
int main()
{
fstream fin("milk6.in",ios::in);
fstream fout("milk6.out",ios::out);
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>st[i]>>en[i]>>c[i];
cost[st[i]][en[i]]+=c[i];
}
fin.close();
for(int k=1;k<=n;k++)
for(int j=1;j<=n;j++)
g[k][j]=cost[k][j];
max_flow=dinic();
fout<<max_flow<<" ";
int sum=0;
for(int i=1;i<=m;i++)
{
for(int k=1;k<=n;k++)
for(int j=1;j<=n;j++)
cost[k][j]=g[k][j];
cost[st[i]][en[i]]-=c[i];
int new_flow=dinic();
if(new_flow<max_flow)
{
idx[0]++;
idx[idx[0]]=i;
sum+=c[i];
}
}
if(sum==max_flow)
{
for(int i=0;i<=idx[0];i++)
fout<<idx[i]<<endl;
fout.close();
return 0;
}
min_num=idx[0];
get_best(1,0,0);
fout<<min_num<<endl;
for(int i=1;i<=idx[0];i++)
if(best[i])
fout<<idx[i]<<endl;
fout.close();
return 0;
}