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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| typedef pair<int, int> PII; const int maxn = 50010, maxm = (maxn + 1000 + 200*1000)*2 + 10; const int inf = 0x3f3f3f3f; int n, m, K, s, t, S, T; vector<PII> tunnel; map<PII, int> mp;
int head[maxn], ver[maxm], e[maxm], ne[maxm], idx = 1; void add(int a, int b, int c) { ver[++idx] = b; e[idx] = c; ne[idx] = head[a]; head[a] = idx; ver[++idx] = a; e[idx] = 0; ne[idx] = head[b]; head[b] = idx; }
inline int get(int day, int i) { return day*(n+1) + i; }
int d[maxn], cur[maxn]; bool bfs() { memset(d, -1, sizeof d); d[S] = 0, cur[S] = head[S]; queue<int> q; q.push(S);
while (q.size()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = ne[i]) { int y = ver[i]; if (d[y] == -1 && e[i]) { d[y] = d[x] + 1; cur[y] = head[y]; if (y == T) return true; q.push(y); } } } return false; }
int dinic(int u, int lim) { if (u == T) return lim; int flow = 0; for (int i = cur[u]; i && lim > flow; i = ne[i]) { cur[u] = i; int v = ver[i]; if (d[v] == d[u] + 1 && e[i]) { int t = dinic(v, min(e[i], lim-flow)); if (!t) d[v] = -1; flow += t, e[i] -= t, e[i^1] += t; } } return flow; }
int dinic() { int res = 0, flow; while (bfs()) while (flow = dinic(S, inf)) res += flow; return res; }
int day = 1; void solve() { add(S, get(0, s), K); add(get(0, t), T, inf); day = 1; int res = 0; while (true) { add(get(day, t), T, inf); for (int i = 1; i <= n; i++) add(get(day-1, i), get(day, i), inf); for (auto x : tunnel) { int u = x.first, v = x.second; add(get(day-1, u), get(day, v), 1); mp[ {get(day-1, u), get(day, v)} ] = idx - 1;
add(get(day-1, v), get(day, u), 1); mp[ {get(day-1, v), get(day, u)} ] = idx - 1; }
res += dinic(); if (res >= K) break; day++; } printf("%d\n", day); }
void out() { vector<int> location(K, s); for (int p = 1; p <= day; p++) { vector<PII> sol; vector<int> moved(K, 0);
for (auto x : tunnel) { int u = x.first, v = x.second; int id1 = mp[ {get(p-1, u), get(p, v)} ]; int id2 = mp[ {get(p-1, v), get(p, u)} ];
int f1 = e[id1 ^ 1], f2 = e[id2 ^ 1]; if (f1 == 1 && f2 == 0) sol.push_back( {u, v} ); if (f1 == 0 && f2 == 1) sol.push_back( {v, u} ); }
printf("%d", (int)sol.size());
vector<PII> ans; for (auto x : sol) { int u = x.first, v = x.second; for (int i = 0; i < K; i++) { if (!moved[i] && location[i] == u) { ans.push_back( {i+1, v} ); moved[i] = true; location[i] = v; break; } } } sort(ans.begin(), ans.end()); for (auto x : ans) printf(" %d %d", x.first, x.second); printf("\n"); } }
int main() { freopen("bring.in", "r", stdin); freopen("bring.out", "w", stdout); scanf("%d%d%d%d%d", &n, &m, &K, &s, &t); S = maxn-1, T = maxn-2; for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); tunnel.push_back({u, v}); }
// solve solve();
// out out(); }
|