Created
April 28, 2013 09:02
-
-
Save ftiasch/5476353 to your computer and use it in GitHub Desktop.
(http://acmore.net/problem.php?id=1509) Edmonds-Karp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstring> | |
#include <climits> | |
#include <vector> | |
#include <queue> | |
#include <algorithm> | |
const int M = 200; | |
int a[M], b[M], c[M]; | |
const int V = M * 2 + 2; | |
int capacity[V][V], back[V]; | |
int solve(int source, int target, int n) { | |
int ret = 0; | |
while (true) { | |
memset(back, -1, sizeof(back)); | |
std::queue <int> queue; | |
queue.push(source); | |
while (!queue.empty()) { | |
int u = queue.front(); | |
queue.pop(); | |
for (int v = 0; v < n; ++ v) { | |
if (capacity[u][v] > 0 && back[v] == -1) { | |
back[v] = u; | |
queue.push(v); | |
} | |
} | |
} | |
if (back[target] == -1) { | |
break; | |
} | |
int delta = INT_MAX; | |
for (int v = target; v != source; v = back[v]) { | |
delta = std::min(delta, capacity[back[v]][v]); | |
} | |
ret += delta; | |
for (int v = target; v != source; v = back[v]) { | |
capacity[back[v]][v] -= delta; | |
capacity[v][back[v]] += delta; | |
} | |
} | |
return ret; | |
} | |
int main() { | |
int source, target; | |
while (scanf("%d%d", &source, &target) == 2) { | |
std::vector <int> values; | |
values.push_back(source); | |
values.push_back(target); | |
int m; | |
scanf("%d", &m); | |
for (int i = 0; i < m; ++ i) { | |
scanf("%d%d%d", a + i, b + i, c + i); | |
values.push_back(a[i]); | |
values.push_back(b[i]); | |
} | |
std::sort(values.begin(), values.end()); | |
values.erase(std::unique(values.begin(), values.end()), values.end()); | |
#define FIND(v) (std::lower_bound(values.begin(), values.end(), v) - values.begin()) | |
memset(capacity, 0, sizeof(capacity)); | |
for (int i = 0; i < m; ++ i) { | |
capacity[FIND(a[i])][FIND(b[i])] += c[i]; | |
} | |
printf("%d\n", solve(FIND(source), FIND(target), (int)values.size())); | |
#undef FIND | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
赞一个!