【bzoj1834】ZJOI2010网络扩容

Description

给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。

求:

1、在不扩容的情况下,1到N的最大流;

2、将1到N的最大流增加K所需的最小扩容费用。

Input

第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。

接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。

N<=1000,M<=5000,K<=10

Output

输出文件一行包含两个整数,分别表示问题1和问题2的答案。

Sample Input

5 8 2

1 2 5 8

2 5 9 9

5 1 6 2

5 1 1 8

1 2 8 7

2 5 4 9

1 2 1 1

1 4 2 1

Sample Output

13 19

Solution

第一问就是最基础的最大流问题,用什么算法都可以。

注意的是要建费用为0的边。

然后对于每条边还要建费用为W,流量为K的边

然后建一个0到1的流量K,费用0的边

从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
#include<bits/stdc++.h>
#define oo 0x7f7f7f7f
#define ll long long
#define reg register int

struct net_flow{
int val, flow, nt, y;
int rev;
}e[101000];
int lin[1010];
int len = 0;
int st, ed;
int n, m, k;
int level[1010];
int q[10100];
int head, tail;
struct input_data{
int x, y, c, v;
}aa[5010];
int dis[1010];
int edge[1010];
int node[1010];
bool vis[1010];

int read() {
int k = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
k = k * 10 + ch - '0';
ch = getchar();
}
return k * f;
}

inline void insert(int x, int y, int val, int flow, int z) {
e[++len].nt = lin[x];
lin[x] = len;
e[len].y = y;
e[len].val = val;
e[len].flow = flow;
e[len].rev = len + z;
}

void init() {
n = read(), m = read(), k = read();
for (reg i = 1; i <= m; ++i) {
int x = read(), y = read();
int flow = read(), val = read();
insert(x, y, 0, flow, 1);
insert(y, x, 0, 0, -1);
aa[i].x = x, aa[i].y = y, aa[i].c = flow, aa[i].v = val;
}
}

bool make_level() {
memset(level, -1, sizeof(level));
level[st] = 0;
head = 1, tail = 0;
q[++tail] = st;
while (head <= tail) {
int x = q[head++];
for (reg i = lin[x]; i; i = e[i].nt) {
int y = e[i].y;
if (e[i].flow && level[y] == -1) {
level[y] = level[x] + 1;
q[++tail] = y;
}
}
}
return level[ed] >= 0;
}

int max_flow(int x, int flow) {
int maxflow = 0;
int d = 0;
if (x == ed) return flow;
for (reg i = lin[x]; i && maxflow < flow; i = e[i].nt) {
int y = e[i].y;
if (level[y] == level[x] + 1 && e[i].flow) {
if (d = max_flow(y, std::min(flow - maxflow, e[i].flow))) {
maxflow += d;
e[i].flow -= d;
e[e[i].rev].flow += d;
}
}
}
if (!maxflow) level[x] = -1;
return maxflow;
}

void dinic() {
st = 1, ed = n;
int ans = 0;
int d = 0;
while (make_level())
while (d = max_flow(st, oo))
ans += d;
printf("%d ", ans);
}

void re_build() {
st = 0, ed = n;
for (reg i = 1; i <= m; ++i) {
int x = aa[i].x, y = aa[i].y;
int flow = k, val = aa[i].v;
insert(x, y, val, flow, 1);
insert(y, x, -val, 0, -1);
}
insert(0, 1, 0, k, 1);
insert(1, 0, 0, 0, -1);
}

bool spfa() {
memset(dis, 0x7f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[st] = 0;
vis[st] = 1;
head = 1, tail = 0;
q[++tail] = st;
while (head <= tail) {
int x = q[head++];
for (reg i = lin[x]; i; i = e[i].nt) {
int y = e[i].y;
if (e[i].flow && dis[x] + e[i].val < dis[y]) {
dis[y] = dis[x] + e[i].val;
node[y] = x;
edge[y] = i;
if (!vis[y]) {
q[++tail] = y;
vis[y] = 1;
}
}
}
vis[x] = 0;
}
return dis[ed] != oo;
}

int agu() {
int flow = oo;
int sum = 0;
for (reg i = ed; i != st; i = node[i]) {
if (e[edge[i]].flow < flow)
flow = e[edge[i]].flow;
}
for (reg i = ed; i != st; i = node[i]) {
int te = edge[i];
e[te].flow -= flow;
int re = e[te].rev;
e[re].flow += flow;
sum += e[te].val * flow;
}
return sum;
}

void cost_flow() {
int ans = 0;
while (spfa())
ans += agu();
printf("%d\n", ans);
}

int main() {
init();
dinic();
re_build();
cost_flow();
return 0;
}