洛谷 P1344. [USACO4.4]追查坏牛奶Pollutant Control

链接

https://www.luogu.com.cn/problem/P1344

题意

求最小割的容量以及最小割的边数

思路

找一个大于边数的数 $a$

建图时所有边的边权 $w$ 改为 $w’=w*a+1$

设新图中最大流为flow,那么原图中最大流为$\lfloor\frac{flow}{a}\rfloor$,即最小割的容量,最小割的边数为 $flow%a$

代码

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
#include<bits/stdc++.h>
using namespace std;
const int N=205;
const int BASE=1200;
int n,m,s,t,d[N],cur[N];
vector<pair<int,long long> > e;
vector<int> G[N];
void addedge(int u,int v,long long w) {
e.push_back(make_pair(v,w));
e.push_back(make_pair(u,0));
G[u].push_back(e.size()-2);
G[v].push_back(e.size()-1);
}
bool bfs() {
for(int i=1;i<=n;i++) d[i]=0;
queue<int> q;
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
for(auto x:G[u]) {
int &v=e[x].first;
long long &w=e[x].second;
if(v==s||d[v]||w<=0) continue;
d[v]=d[u]+1;
q.push(v);
}
}
return d[t];
}
long long dfs(int u,long long a) {
if(u==t) return a;
long long f,flow=0;
for(int &i=cur[u];i<G[u].size();i++) {
int &v=e[G[u][i]].first;
long long &w=e[G[u][i]].second;
if(d[v]!=d[u]+1||w<=0||(f=dfs(v,min(a,w)))<=0) continue;
w-=f;
e[G[u][i]^1].second+=f;
a-=f;
flow+=f;
if(a==0) break;
}
return flow;
}
long long dinic() {
long long res=0;
while(bfs()) {
for(int i=1;i<=n;i++) cur[i]=0;
res+=dfs(s,0x7fffffff);
}
return res;
}
int main() {
scanf("%d%d",&n,&m);
int u,v;
long long w;
for(int i=1;i<=m;i++) {
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w*BASE+1);
}
s=1,t=n;
long long res=dinic();
printf("%lld %lld\n",res/BASE,res%BASE);
return 0;
}