Blog of RuSun

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

P3872 [TJOI2010]电影迷

P3872 [TJOI2010]电影迷

最大权闭合子图。

按照套路即可。源点连正权点,负权点连汇点,流量为绝对值,答案为正权点之和减最小割。当前答案为所有正权点,对于一条边,要么放弃左部的正权点(放弃正权),要么选择右部的负权点(减去负权的绝对值),要么付出相应的代价(减去代价),这些都是减少的,所以正权点之和减去最小割即答案。

查看代码
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
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define inf INT_MAX
using namespace std;
const int N = 110, M = 5e4 + 10;
int n, m, st, ed, tot, d[N], cur[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 exploit(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 = 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;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = exploit(st, inf))
res += flow;
return res;
}
void add (int a, int b, int c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
int main ()
{
cin >> n >> m;
st = 0;
ed = n + 1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1, a; i <= n; i++)
{
cin >> a;
if (a > 0)
{
tot += a;
add(st, i, a);
add(i, st, 0);
}
else if (a < 0)
{
add(i, ed, -a);
add(ed, i, 0);
}
}
for (int i = 1, a, b, c; i <= m; i++)
{
cin >> a >> b >> c;
add(a, b, c);
add(b, a, 0);
}
cout << tot - dinic();
return 0;
}