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
| #include <bits/stdc++.h> #define MAX_N 500 #define MAX_M 800 #define INF 0x7f7f7f7f using namespace std; int n, m, id, s, t, tc, cnt, pr[MAX_N+5], d[MAX_N+5]; struct node {int u, v, c, nxt;} E[MAX_M*10]; struct edge {int u, v, c, id;} e[MAX_M+5]; bool cmp(const edge &a, const edge &b) {return a.c < b.c;} void init() {s = e[id].u, t = e[id].v, tc = e[id].c; memset(pr, -1, sizeof pr);} void insert(int u, int v, int c) {E[cnt].v = v, E[cnt].c = c, E[cnt].nxt = pr[u], pr[u] = cnt++;} void addedge(int u, int v, int c) {insert(u, v, c), insert(v, u, 0);} bool BFS() { queue <int> que; que.push(s); memset(d, -1, sizeof d), d[s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c; if (!c || ~d[v]) continue; d[v] = d[u]+1, que.push(v); } } return ~d[t]; } int DFS(int u, int flow) { if (u == t) return flow; int ret = 0; for (int i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c; if (!c || d[v] != d[u]+1) continue; int tmp = DFS(v, min(flow, c)); E[i].c -= tmp, E[i^1].c += tmp; flow -= tmp, ret += tmp; if (!flow) break; } if (!ret) d[u] = -1; return ret; } int Dinic() {int ret = 0; while (BFS()) ret += DFS(s, INF); return ret;} int main() { scanf("%d%d%d", &n, &m, &id); for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].c), e[i].id = i; init(), sort(e+1, e+m+1, cmp); for (int i = 1; i <= m; i++) if (e[i].id^id) { if (e[i].c > tc) break; addedge(e[i].u, e[i].v, tc-e[i].c+1); addedge(e[i].v, e[i].u, tc-e[i].c+1); } printf("%d", Dinic()); return 0; }
|