constint inf = 0x3f3f3f3f; constint N = 2e4 + 10; int n, m, cnt = 1; int head[N], dep[N]; bool vis[N];
structEdge { int to, nxt, val; } e[N];
voidaddEdge(int u, int v, int w){ e[++cnt].to = v; e[cnt].nxt = head[u]; e[cnt].val = w; head[u] = cnt; return ; }
boolcheck(){ for (int i=0; i<=n+1; ++i) dep[i] = 0; dep[0] = 2; queue <int> sp; sp.push(0); while (!sp.empty()) { int cur = sp.front(); sp.pop(); for (int i=head[cur]; i; i=e[i].nxt) { int to = e[i].to; if (!dep[to] && e[i].val) { dep[to] = dep[cur] + 1, sp.push(to); if (to==n+1) returntrue; } } } returnfalse; }
intdfs(int cur, int Max){ if (cur==n+1) return Max; int increase = Max; for (int i=head[cur]; i&&increase; i=e[i].nxt) { int to = e[i].to; if (e[i].val && dep[to]==dep[cur]+1) { int res = dfs(to, min(increase, e[i].val)); e[i].val -= res, e[i^1].val += res; if (!res) dep[to] = 0; increase -= res; } } return Max-increase; }
voidread(int &ret){ ret = 0; char ch = getchar(); int f = 1; while (!isdigit(ch) && ch^'-') ch = getchar(); if (ch=='-') f = -1, ch = getchar(); while (isdigit(ch)) { ret = (ret<<1) + (ret<<3) + (ch^48); ch = getchar(); } ret *= f; return ; }
intmain(){ read(m), read(n); while (1) { int u, v; read(u), read(v); if (u==-1 && v==-1) break; addEdge(u, v, inf); addEdge(v, u, 0); } for (int i=1; i<=m; ++i) addEdge(0, i, 1), addEdge(i, 0, 0); for (int i=m+1; i<=n; ++i) addEdge(i, n+1, 1), addEdge(n+1, i, 0); int ans = 0; while(check()) ans += dfs(0, inf); cout << ans << endl; for (int i=2; i<=cnt; i+=2) { if (!e[i^1].val) continue; if (!e[i].to || !e[i^1].to) continue; if (e[i].to==n+1) continue; if (e[i^1].to==n+1) continue; printf("%d %d\n", e[i^1].to, e[i].to); } return0; }
int n, m, p, k, s, door[N][N][N][N]; int cnt[N][N], key[N][N][N]; bool vis[N][N][1<<N];
structspread { int x, y, Key, cost; spread(int x, int y, int Key, int cost): x(x), y(y), Key(Key), cost(cost) {} spread() {} };
intkeyset(int x, int y){ int S = 0; for (int i=1; i<=cnt[x][y]; ++i) S |= (1<<(key[x][y][i]-1)); return S; }
boolcheck(int x, int y, int k, int st){ if (x<1 || x>n) returnfalse; if (y<1 || y>m) returnfalse; if (k<0 || (k && !(st&(1<<(k-1))))) returnfalse; returntrue; }
intbfs(){ int x = 1, y = 1; int S = keyset(x, y); queue <spread> que; que.push(spread(x, y, S, 0)); vis[x][y][S] = true; while (!que.empty()) { spread cur = que.front(); que.pop(); x = cur.x, y = cur.y, S = cur.Key; if (x==n && y==m) return cur.cost; for (int i=0; i<4; ++i) { int tox = x + dx[i], toy = y + dy[i]; int Door = door[x][y][tox][toy]; if (!check(tox, toy, Door, S)) continue; int to = S|keyset(tox, toy); if (vis[tox][toy][to]) continue; vis[tox][toy][to] = true; que.push(spread(tox, toy, to, cur.cost+1)); } } return-1; }
intmain(){ cin >> n >> m >> p >> k; while (k--) { int x, y, X, Y, G; cin >> x >> y >> X >> Y >> G; if (G) door[x][y][X][Y] = door[X][Y][x][y] = G; else door[x][y][X][Y] = door[X][Y][x][y] = -1; } cin >> s; while (s--) { int x, y, Q; cin >> x >> y >> Q; key[x][y][++cnt[x][y]] = Q; } cout << bfs() << endl; return0; }
boolcheck(){ for (int i=0; i<=t; ++i) dep[i] = inf, inq[i] = false; dep[s] = 0; queue <int> sp; sp.push(s); inq[s] = true; while (!sp.empty()) { int cur = sp.front(); sp.pop(); for (int i=head[cur]; i; i=e[i].nxt) { int to = e[i].to; if (e[i].flow && dep[to]==inf) { dep[to] = dep[cur] + 1, sp.push(to); } } } return dep[t] ^ inf; }
intdfs(int cur, int Max){ if (cur==t) return Max; int increase = Max; for (int i=head[cur]; i && increase; i=e[i].nxt) { int to = e[i].to; if (e[i].flow && dep[to]==dep[cur]+1) { int res = dfs(to, min(increase, e[i].flow)); if (!res) { dep[to] = -1; continue; } e[i].flow -= res, e[i^1].flow += res; path[cur] = to; if (cur) vis[to-n] = true; increase -= res; } } return Max-increase; }
intmain(){ cin >> n >> m; t = 2*n+1; for (int i=1; i<=m; ++i) { int u, v; cin >> u >> v; addEdge(u, n+v, 1); } for (int i=1; i<=n; ++i) addEdge(s, i, 1); for (int i=1; i<=n; ++i) addEdge(i+n, t, 1); int ans = 0; while (check()) ans += dfs(s, inf); for (int i=1; i<=n; ++i) { if (vis[i]) continue; int cur = i; cout << i; while(path[cur] && path[cur]^t) cout << ' ' << path[cur]-n, cur = path[cur]-n; cout << endl; } cout << n-ans << endl; return0; }
#include<stdio.h> int n, sum, ans, a[110], Sum[110];
intabs(int x){ return x>0? x:-x; }
voidqsort(int l, int r){ int mid = Sum[(l+r)>>1]; int i=l, j=r; do { while (Sum[i]<mid) ++i; while (Sum[j]>mid) --j; if(i<=j) { int u=Sum[i]; Sum[i]=Sum[j]; Sum[j]=u; i++, j--; } } while (i<=j); if (l<j) qsort(l, j); if (i<r) qsort(i, r); return ; }
intmain(){ scanf("%d", &n); for (int i=1; i<=n; ++i) scanf("%d", &a[i]), sum += a[i]; sum /= n; for (int i=1; i<=n; ++i) Sum[i] = Sum[i-1]+a[i]-sum, a[i] = Sum[i]; qsort(1, n); int mid = n/2+1; for (int i=1; i<=n; ++i) ans += abs(Sum[mid]-Sum[i]); printf("%d\n", ans); return0; }