Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

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

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

基本上是裸的最小割,额外的问题是,最最小割的前提下,要保证割的边数最少。

奇技淫巧:设一条边的权值为 $w$ ,将其修改修改为 $w mod + 1$ ,将边权和边数压缩为一个数。

查看代码
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
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define inf INT_MAX
using namespace std;
typedef long long LL;
const int N = 40, M = 2e3 + 10, MOD = 1e3 + 1;
int n, m, st, ed, d[N], cur[N];
int idx = -1, hd[N], nxt[M], edg[M];
LL wt[M];
bool bfs()
{
for (int i = st; i <= ed; i++)
d[i] = -1;
d[st] = 0;
cur[st] = hd[st];
queue <int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
if (d[edg[i]] == -1 && wt[i])
{
cur[edg[i]] = hd[edg[i]];
d[edg[i]] = d[t] + 1;
if (edg[i] == ed)
return true;
q.push(edg[i]);
}
}
return false;
}
LL exploit(int x, LL limit)
{
if (x == ed)
return limit;
LL res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + 1 && wt[i])
{
LL t = exploit(edg[i], min(wt[i], limit - res));
if (!t)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
LL dinic()
{
LL res = 0, flow;
while (bfs())
while (flow = exploit(st, inf))
res += flow;
return res;
}
void add (int a, int b, LL c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
int main ()
{
cin >> n >> m;
st = 1;
ed = n;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1, a, b, c; i <= m; i++)
{
cin >> a >> b >> c;
add(a, b, c * MOD + 1);
add(b, a, 0);
}
LL t = dinic();
cout << t / MOD << ' ' << t % MOD;
return 0;
}