abcdeffa's Blog

当局者迷,旁观者清。

0%

GMOJ S3993 【Magic】

Description

给你一个有 $n$ 个点 $m$ 条边的有向图,对于任意一个点 $i$,都有两个权值 $a_i$ 和 $b_i$,你可以花费 $b_i$ 的费用将这个点的 $a_i$ 变成 0。另外,对于图中的每个点你需要付出 $w_i = \max_{(i, j) \in E} a_j$ 的费用。求代价和的最小值。

Solution

考虑使用 网络流 来解题。

因为最大流等于最小割,所以我们可以想出连边方式:

为了方便表述,下文用 $i’$ 来表示点 $i$ 拆出去的点(可能有多个)。

对于每个点 $i$,连一条 $S$ 到 $i$ ,容量为 $b_i$ 的边。

对于点 $u$ 连出去的所有点 $v_1, v_2, …, v_k(a_{v_1} \leq a_{v_2} \leq … \leq a_{v_k})$,连一条从 $v_i’$ 从 $v_{i + 1}’(1 \leq i < k)$,容量为 $a_{v_i}’$ 的边。

对于点 $v_k’$,连一条从 $v_k’$ 到 $T$ 的容量为 $a_{v_k}$ 的边。

对于每个点 $i$,连一条从 $i$ 到 $i’$ (可能有多个 $i’$,全部都要连)的流量为正无穷的边。

其中 $S$ 表示的是原点,$T$ 表示的是汇点。

想一想,如果我们割掉了一条容量为 $a_i$ 的边,表示我们让它当最大值,若割掉了一条边权为 $b_i$ 的边,则表示让 $a_i = 0$,此时则需要割掉一条边权为 $a_j(a_j \leq a_i)$ 的边来使汇点的流量为 0,即一个割。

那么正确性显然。

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <cstdio>
#include <cstring>
#define maxN 1010
#define maxM 300010
#define inf 2147483647
struct edge{ int x, y, c, g; } b[maxM << 1];
int len = 1, S = 0, T = 0;
int dep[maxM << 1], f[maxM << 1];
int A[maxN], B[maxN], X[maxM], Y[maxM], h[maxM << 1];
int min (int x, int y)
{
return x < y ? x : y;
}
void ins (int x, int y, int c)
{
len++;
b[len].x = x;
b[len].y = y;
b[len].c = c;
b[len].g = h[x];
h[x] = len;

len++;
b[len].x = y;
b[len].y = x;
b[len].c = 0;
b[len].g = h[y];
h[y] = len;
}
void px (int l, int r)
{
int x = l, y = r, mid1 = X[(l + r) >> 1], mid2 = A[Y[(l + r) >> 1]];
while(x <= y)
{
while(X[x] < mid1 || (X[x] == mid1 && A[Y[x]] < mid2))
{
x++;
}
while(X[y] > mid1 || (X[y] == mid1 && A[Y[y]] > mid2))
{
y--;
}
if(x <= y)
{
int t = X[x];
X[x] = X[y];
X[y] = t;

t = Y[x];
Y[x] = Y[y];
Y[y] = t;

x++;
y--;
}
}
if(l < y)
{
px(l, y);
}
if(x < r)
{
px(x, r);
}
}
bool bfs ()
{
const int M = (maxM << 1);
int tou = 1, wei = 2;
f[1] = S;
dep[S] = 1;
while(tou != wei)
{
int x = f[tou];
for(register int i = h[x];i;i = b[i].g)
{
int y = b[i].y;
if(b[i].c && !dep[y])
{
dep[y] = dep[x] + 1;
f[wei] = y;
wei++;
if(wei >= M)
{
wei = 1;
}
if(y == T)
{
return true;
}
}
}
tou++;
if(tou >= M)
{
tou = 1;
}
}
return false;
}
int dfs (int x, int flow)
{
if(x == T)
{
return flow;
}
int now = flow;
for(register int i = h[x];i;i = b[i].g)
{
int y = b[i].y;
if(b[i].c && dep[y] == dep[x] + 1)
{
int ma = min(now, b[i].c);
now -= ma;
b[i].c -= ma;
b[i ^ 1].c += ma;
int rest = ma - dfs(y, ma);
now += rest;
b[i].c += rest;
b[i ^ 1].c -= rest;
}
}
return flow - now;
}
int main ()
{
int n = 0, m = 0;
scanf("%d %d", &n, &m);
T = n + m + 1;
for(int i = 1;i <= n; i++)
{
scanf("%d", &A[i]);
}
for(int i = 1;i <= n; i++)
{
scanf("%d", &B[i]);
}
for(int i = 1;i <= m; i++)
{
scanf("%d %d", &X[i], &Y[i]);
}
px(1, m);
for(int i = 1;i <= n; i++)
{
ins(S, i, B[i]);
}
int now = 1, cnt = n;
for(int i = 1;i <= n; i++)
{
while(X[now] < i && now <= m)
{
now++;
}
if(now > m)
{
continue;
}
int last = 0;
int Yz = 0, Az = 0;
while(X[now] == i && now <= m)
{
cnt++;
if(last)
{
ins(last, cnt, Az);
}
last = cnt;
Yz = Y[now];
Az = A[Y[now]];
ins(Yz, cnt, inf);
now++;
}
if(last)
{
ins(cnt, T, Az);
}
}
int Ans = 0;
while(bfs())
{
Ans += dfs(S, inf);
memset(dep, 0, sizeof(dep));
}
printf("%d", Ans);
return 0;
}