Blog of RuSun

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

P4177 [CEOI2008]order

P4177 [CEOI2008]order

最大权闭合子图,不一样的是,原来的模型中,一个机器要么买要么不买,一个工作要么做要么不做,但是现在可以租机器做工作,实质上是花费特定的代价让一个机器只做一个工作。原来机器和工作中间只能连接 $inf$ ,因为我们只允许要么放弃工作的价值要么放弃机器的代价,不允许一个机器和一个工作间割,可以发现,此代价可以形象为租机器的代价。

查看代码
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
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define INF INT_MAX
using namespace std;
const int N = 2410, M = 5e7 + 10;
int n, m, st, ed, tot, d[N], cur[N];
bool vis[N];
int idx = -1, hd[N], nxt[M], edg[M], 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;
}
int find(int x, int limit)
{
if (x == ed)
return limit;
int 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])
{
int t = find(edg[i], min(wt[i], limit - res));
if (!t)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = find(st, INF))
res += flow;
return res;
}
void add (int x, int y, int z)
{
nxt[++idx] = hd[x];
hd[x] = idx;
edg[idx] = y;
wt[idx] = z;
}
int main ()
{
cin >> n >> m;
st = 0;
ed = n + m + 1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1, a, b, c, d; i <= n; i++)
{
cin >> a >> b;
add(st, i, a);
add(i, st, 0);
tot += a;
for (int j = 1; j <= b; j++)
{
cin >> c >> d;
add(i, c + n, d);
add(c + n, i, 0);
}
}
for (int i = n + 1, a; i < ed; i++)
{
cin >> a;
add(i, ed, a);
add(ed, i, 0);
}
cout << tot - dinic();
return 0;
}