inlineintsap(int v, int flow, int s, int t, int n){ if (v == t) return flow; staticint iter[MAXN]; registerint rec = 0; for (registerint i = iter[v]; i < edge[v].size(); i++) { Node *p = &edge[v][i]; if (p->f > 0 && h[v] == h[p->v] + 1) { registerint ret = sap(p->v, std::min(flow - rec, p->f), s, t, n); p->f -= ret, edge[p->v][p->index].f += ret, iter[v] = i; if ((rec += ret) == flow) return flow; } } if (!(--gap[h[v]])) h[s] = n; gap[++h[v]]++, iter[v] = 0; return rec; }
inlineintsap(int s, int t, int n){ registerint ret = 0; gap[0] = n; while (h[s] < n) ret += sap(s, INF, s, t, n); return ret; }
inlinevoidsolve(){ usingnamespace IO; registerint n, m; read(m), read(n); staticint buc[MAXN]; registerint s = 0, t = n * 2 + m + 1; for (registerint i = 1, a; i <= m; i++) read(a), addEdge(buc[i] = i + n, t, a); for (registerint i = 1; i <= n; i++) { registerint q, a; read(q); while (q--) read(a), addEdge(i + n + m, buc[a], INF), buc[a] = i + n + m; addEdge(i, i + n + m, INF); read(a), addEdge(s, i, a); } printf("%d", sap(s, t, t + 1)); } }
inlineintsap(int v, int flow, int s, int t, int n){ if (v == t) return flow; staticint iter[MAXN]; registerint rec = 0; for (registerint i = iter[v]; i < edge[v].size(); i++) { Node *p = &edge[v][i]; if (p->f > 0 && h[v] == h[p->v] + 1) { registerint ret = sap(p->v, std::min(flow - rec, p->f), s, t, n); p->f -= ret, edge[p->v][p->index].f += ret, iter[v] = i; if ((rec += ret) == flow) return flow; } } if (!(--gap[h[v]])) h[s] = n; gap[++h[v]]++, iter[v] = 0; return rec; }
inlineintsap(int s, int t, int n){ registerint ret = 0; memset(gap, 0, sizeof(int) * (n + 1)); memset(h, 0, sizeof(int) * (n + 1)); gap[0] = n; while (h[s] < n) ret += sap(s, INF, s, t, n); return ret; }
long d[MAXN][MAXN]; int n, m, s, t, sum; int a[MAXN], b[MAXN];
inlineboolcheck(long x){ s = 0, t = 2 * n + 1; /* 记得从 0 开始清空 */ for (registerint i = s; i <= t; i++) edge[i].clear(); for (registerint i = 1; i <= n; i++) addEdge(s, i, a[i]), addEdge(n + i, t, b[i]); for (registerint i = 1; i <= n; i++) for (registerint j = 1; j <= n; j++) if (d[i][j] <= x) addEdge(i, j + n, INF); return sap(s, t, t + 1) == sum; }
inlinevoidsolve(){ usingnamespace IO; read(n), read(m); for (registerint i = 1; i <= n; i++) read(a[i]), read(b[i]), sum += a[i]; for (registerint i = 1; i <= n; i++) for (registerint j = 1; j <= n; j++) d[i][j] = INF * 200ll; for (registerint i = 1, u, v; i <= m; i++) { registerlong w; read(u), read(v), read(w); d[u][v] = d[v][u] = std::min(d[u][v], w); } for (registerint i = 1; i <= n; i++) d[i][i] = 0; for (registerint k = 1; k <= n; k++) for (registerint i = 1; i <= n; i++) for (registerint j = 1; j <= n; j++) d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]); long l = 0, r = INF * 200ll - 1, ans = -1; while (l <= r) { registerlong mid = l + r >> 1; if (check(mid)) r = mid - 1, ans = mid; else l = mid + 1; } std::cout << ans; } }
inlineintsap(int v, int flow, int s, int t, int n){ if (v == t) return flow; registerint rec = 0; staticint iter[MAXN]; for (registerint i = iter[v]; i < edge[v].size(); i++) { Node *p = &edge[v][i]; if (p->f > 0 && h[v] == h[p->v] + 1) { registerint ret = sap(p->v, std::min(flow - rec, p->f), s, t, n); p->f -= ret, edge[p->v][p->index].f += ret, iter[v] = i; if ((rec += ret) == flow || h[s] >= n) return rec; } } if (!(--gap[h[v]])) h[s] = n; gap[++h[v]]++, iter[v] = 0; return rec; }
inlineintsap(int s, int t, int n){ registerint ret = 0; gap[0] = n; while (h[s] < n) ret += sap(s, INF, s, t, n); return ret; }
inlinevoidsolve(){ usingnamespace IO; registerint nl, nr, m, s = 0; read(nl), read(nr), read(m); registerint t = nl + nr + 1; for (registerint i = 1; i <= nl; i++) addEdge(s, i, 1); for (registerint i = 1; i <= nr; i++) addEdge(i + nl, t, 1); for (registerint i = 1, u, v; i <= m; i++) { read(u), read(v), addEdge(u, v + nl, 1); } print(sap(s, t, t + 1)), print('\n'); for (registerint i = 1, x, p; i <= nl; print(x), print(' '), i++) { for (x = 0, p = 0; p < edge[i].size(); p++) { if (!edge[i][p].f && edge[i][p].v != s) { x = edge[i][p].v - nl; break; } } } } }
inlineintsap(int v, int flow, int s, int t, int n){ if (v == t) return flow; registerint rec = 0; staticint iter[MAXN]; for (registerint i = iter[v]; i < edge[v].size(); i++) { Node *p = &edge[v][i]; if (p->f > 0 && h[v] == h[p->v] + 1) { registerint ret = sap(p->v, std::min(flow - rec, p->f), s, t, n); p->f -= ret, edge[p->v][p->index].f += ret, iter[v] = i; if ((rec += ret) == flow || h[s] >= n) return rec; } } if (!(--gap[h[v]])) h[s] = n; gap[++h[v]]++, iter[v] = 0; return rec; }
inlineintsap(int s, int t, int n){ registerint ret = 0; gap[0] = n; while (h[s] < n) ret += sap(s, INF, s, t, n); return ret; }
bool vis[MAXN];
int n;
inlinevoiddfs(int v){ IO::print(v = v > n ? v - n : v), IO::print(' '); vis[v] = true; for (registerint i = 0; i < edge[v].size(); i++) { Node *p = &edge[v][i]; if (!p->f && p->v != 0 && !vis[p->v]) { dfs(p->v); break; } } }
inlinevoidsolve(){ usingnamespace IO; registerint m; read(n), read(m); registerint s = 0, t = n << 1 | 1; for (registerint i = 1; i <= n; i++) addEdge(s, i, 1), addEdge(i + n, t, 1); for (registerint i = 0, u, v; i < m; i++) read(u), read(v), addEdge(u, v + n, 1); registerint ans = sap(s, t, t + 1); for (registerint i = 1; i <= n; i++) { if (!vis[i]) { dfs(i), print('\n'); } } print(n - ans), print('\n'); } }
inlineintsap(int v, int flow, int s, int t, int n){ if (v == t) return flow; registerint rec = 0; staticint iter[MAXN]; for (registerint i = iter[v]; i < edge[v].size(); i++) { Node *p = &edge[v][i]; if (p->f > 0 && h[v] == h[p->v] + 1) { registerint ret = sap(p->v, std::min(flow - rec, p->f), s, t, n); p->f -= ret, edge[p->v][p->index].f += ret, iter[v] = i; if ((rec += ret) == flow || h[v] >= n) return rec; } } if (!(--gap[h[v]])) h[s] = n; gap[++h[v]]++, iter[v] = 0; return rec; }
inlineintsap(int s, int t, int n){ registerint ret = 0; gap[0] = n; while (h[s] < n) ret += sap(s, INT_MAX, s, t, n); return ret; }
inlinevoidsolve(){ usingnamespace IO; registerint n, m; read(n), read(m); constint s = 0, t = n + m + 1; registerint sum = 0; for (registerint i = 1, p; i <= n; i++) read(p), addEdge(i, t, p); for (registerint i = 1, u, v, c; i <= m; i++) { read(u), read(v), read(c); sum += c, addEdge(s, i + n, c); addEdge(i + n, u, INT_MAX), addEdge(i + n, v, INT_MAX); } std::cout << sum - sap(s, t, t + 1); } }