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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include<bits/stdc++.h> using namespace std;
struct Edge { int u, v; };
const int MAXN = 5000 + 5; const int MAXM = 200000 + 5; const int MAXK = 50 + 5; const int INF = INT_MAX;
Edge e[MAXM]; pair<int, int> mov[MAXM]; queue<int> q; int pos[MAXK]; bool blk[MAXK]; int head[MAXN], to[MAXM], nxt[MAXM], flow[MAXM], cap[MAXM]; int tot = 1; int dep[MAXN], cur[MAXN]; int n, m, k, s, T, t; int cnt, ans;
void init() { memset(head, 0, sizeof(head)); tot = 1; }
void addEdge(int u, int v, int c, int f) { tot++; to[tot] = v; nxt[tot] = head[u]; flow[tot] = f; cap[tot] = c; head[u] = tot; }
bool bfs() { memset(dep, 0, sizeof(dep)); while(!q.empty()) q.pop(); q.push(s); dep[s] = 1; while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i != 0; i = nxt[i]) { if(cap[i] > flow[i] && dep[to[i]] == 0) { dep[to[i]] = dep[u] + 1; q.push(to[i]); if(to[i] == t) return true; } } } return false; }
int dinic(int u, int limit) { if(u == t || limit == 0) { return limit; } int rest = limit; for(int& i = cur[u]; i != 0; i = nxt[i]) { if(cap[i] > flow[i] && dep[to[i]] == dep[u] + 1) { int k = dinic(to[i], min(rest, cap[i] - flow[i])); if(k == 0) { dep[to[i]] = 0; } flow[i] += k; flow[i ^ 1] -= k; rest -= k; if(rest == 0) break; } } return limit - rest; }
int maxflow(int limit) { int res = 0; while(bfs()) { memcpy(cur, head, sizeof(head)); res += dinic(s, limit - res); if(res == limit) { break; } } return res; }
void output() { for(int i = 1; i <= k; i++) { pos[i] = s; } int now = 2; for(int i = 1; i <= cnt; i++) { memset(blk, 0, sizeof(blk)); ans = 0; for(int j = 1; j <= m; j++) { if(flow[now] == 1 && flow[now + 2] == 0) { mov[++ans] = make_pair(e[j].u, e[j].v); } if(flow[now] == 0 && flow[now + 2] == 1) { mov[++ans] = make_pair(e[j].v, e[j].u); } now += 4; } now += n * 2; printf("%d", ans); for(int j = 1; j <= ans; j++) { for(int p = 1; p <= k; p++) { if(blk[p]) { continue; } if(pos[p] == mov[j].first) { pos[p] = mov[j].second; printf(" %d %d", p, pos[p]); blk[p] = true; break; } } } printf("\n"); } }
int main() { while(scanf("%d%d%d%d%d", &n, &m, &k, &s, &T) == 5) { init(); for(int i = 1; i <= m; i++) { scanf("%d%d", &e[i].u, &e[i].v); } cnt = 0; int sum = 0; while(sum < k) { cnt++; t = cnt * n + T; for(int i = 1; i <= m; i++) { addEdge(e[i].u + (cnt - 1) * n, e[i].v + cnt * n, 1, 0); addEdge(e[i].v + cnt * n, e[i].u + (cnt - 1) * n, 0, 0); addEdge(e[i].v + (cnt - 1) * n, e[i].u + cnt * n, 1, 0); addEdge(e[i].u + cnt * n, e[i].v + (cnt - 1) * n, 0, 0); } for(int i = 1; i <= n; i++) { addEdge(i + (cnt - 1) * n, i + cnt * n, INF, 0); addEdge(i + cnt * n, i + (cnt - 1) * n, 0, 0); } sum += maxflow(k - sum); } printf("%d\n", cnt); output(); }
return 0; }
|