Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define N 200000
- #define M 200000
- #define INF 1000000000
- #include <iostream>
- #include <cstring>
- #include <vector>
- #include <queue>
- using namespace std;
- struct edge {
- int from, to, c, f;
- };
- long long flow;
- int n, m, s, t, d[N], ptr[N], q[N];
- vector<edge> e;
- vector<int> g[N];
- void add (int a, int b, int cost) {
- edge e1 = { a, b, cost, 0 };
- edge e2 = { b, a, 0, 0 };
- g[a].push_back ((int) e.size());
- e.push_back (e1);
- g[b].push_back ((int) e.size());
- e.push_back (e2);
- }
- bool bfs() {
- int qh = 0, qt = 0;
- q[qt++] = s;
- memset (d, -1, n * sizeof d[0]);
- d[s] = 0;
- while (qh < qt && d[t] == -1) {
- int v = q[qh++];
- int len = g[v].size();
- for (size_t i=0; i < len; ++i) {
- int id = g[v][i],
- ed = e[id].to;
- if (d[ed] == -1 && e[id].f < e[id].c) {
- q[qt++] = ed;
- d[ed] = d[v] + 1;
- }
- }
- }
- return d[t] != -1;
- }
- int dfs (int v, int F) {
- if (!F) return 0;
- if (v == t) return F;
- int len = g[v].size();
- for (; ptr[v] < len; ++ptr[v]) {
- int id = g[v][ptr[v]],
- ed = e[id].to;
- if (d[ed] != d[v] + 1) continue;
- int count = dfs (ed, min (F, e[id].c - e[id].f));
- if (count) {
- e[id].f += count;
- e[id ^ 1].f -= count;
- return count;
- }
- }
- return 0;
- }
- int main() {
- // freopen ("in.txt", "r", stdin);
- // freopen ("out.txt", "w", stdout);
- cin >> n >> m;
- s = 0;
- t = n - 1;
- for (int i = 0; i < m; ++i){
- int a, b, cost;
- cin >> a >> b >> cost;
- --a, --b;
- add(a, b, cost);
- }
- int f = 0;
- while (1) {
- if (!bfs()) break;
- memset (ptr, 0, n * sizeof ptr[0]);
- while (int count = dfs (s, INF))
- flow += count;
- }
- cout << flow << endl;
- for (int i = 0; i < m; ++i){
- cout << e[i * 2].f << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement