-
Notifications
You must be signed in to change notification settings - Fork 0
/
11405_mcmf.cpp
107 lines (106 loc) · 2.3 KB
/
11405_mcmf.cpp
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
// 200905 #11405 책 구매하기 Platinum IV
// MCMF 알고리즘
// 유량만큼 비용이 곱해지는 것 주의, 그 외는 Standard
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = 202;
const int INF = 1000000007;
int N, M, ans, w;
struct Edge {
// mcmf일 땐 cost 추가
int to, c, f, cost;
Edge* rev;
Edge() :Edge(-1, 0, 0) {}
Edge(int to1, int c1, int cost1)
:to(to1), c(c1), f(0), cost(cost1), rev(nullptr) {}
int remain() {
return c - f;
}
void push(int x) {
f += x;
rev->f -= x;
}
};
vector<Edge*> v[MAX];
// 용량이 c, 비용이 d인 a->b 간선 생성
inline void addEdge(int a, int b, int c, int d) {
Edge* e1 = new Edge(b, c, d);
Edge* e2 = new Edge(a, 0, -d);
e1->rev = e2;
e2->rev = e1;
v[a].push_back(e1);
v[b].push_back(e2);
}
void mcmf(int s, int e) {
while (1) {
int prev[MAX], d[MAX]; // d: 비용 기록
Edge* path[MAX] = { 0 };
bool inQ[MAX] = { 0 }; // 큐에 정점이 있는가
fill(prev, prev + MAX, -1);
fill(d, d + MAX, INF); // 최소비용이므로 INF로 초기화
queue<int> q;
q.push(s);
d[s] = 0;
inQ[s] = true;
while (!q.empty()) {
int x = q.front();
q.pop();
inQ[x] = false; // 큐에서 꺼냄 (SPFA)
for (Edge* i : v[x]) {
int next = i->to;
// feat.벨만 포드
if (i->remain() > 0 && d[next] > d[x] + i->cost) {
prev[next] = x;
path[next] = i;
d[next] = d[x] + i->cost;
if (!inQ[next]) {
q.push(next);
inQ[next] = true;
}
}
}
}
if (prev[e] == -1)
break;
int flow = INF;
for (int i = e; i != s; i = prev[i]) {
flow = min(flow, path[i]->remain());
}
for (int i = e; i != s; i = prev[i]) {
path[i]->push(flow);
// flow만큼 곱해지므로
w += flow * path[i]->cost; // 비용 갱신
}
ans += flow;
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
// S: 0, 서점: 1~100, 사람: 101~200, E: 201
int S = 0, E = 201;
for (int i = 1; i <= N; i++) {
int k;
cin >> k;
addEdge(i + 100, E, k, 0);
}
for (int i = 1; i <= M; i++) {
int k;
cin >> k;
addEdge(S, i, k, 0);
}
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++) {
int c;
cin >> c;
addEdge(i, j + 100, INF, c);
}
}
mcmf(S, E);
cout << w << "\n";
}