bzoj4842 [Neerc2016]Delight for a Cat
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4842
【题解】
同 http://www.cnblogs.com/galaxies/p/20170627_c.html
输出一个方案就行了
# include <queue> # include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int N = 1e5 + 10, M = 1e5 + 10; const ll inf = 1e17 + 10; int n, K, SS, EE; int s[M], e[M]; int S, T, id[N]; int head[M], nxt[M], to[M], w[M], flow[M], tot = 1; inline void add(int u, int v, int fl, int _w) { ++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; flow[tot] = fl, w[tot] = _w; } inline void adde(int u, int v, int fl, int _w) { add(u, v, fl, _w); add(v, u, 0, -_w); } namespace MCF { queue<int> q; int pre[M]; ll d[M]; bool vis[M]; inline bool spfa() { while(!q.empty()) q.pop(); for (int i=1; i<=T; ++i) vis[i] = 0, d[i] = inf; vis[S] = 1; d[S] = 0; q.push(S); while(!q.empty()) { int top = q.front(); q.pop(); vis[top] = 0; for (int i=head[top]; i; i=nxt[i]) { if(d[to[i]] > d[top] + w[i] && flow[i]) { d[to[i]] = d[top] + w[i]; pre[to[i]] = i; if(!vis[to[i]]) { vis[to[i]] = 1; q.push(to[i]); } } } } return d[T] < inf; } inline ll mcf() { int fl = 1e9; ll ret = 0; for (int i=pre[T]; i; i=pre[to[i^1]]) fl = min(fl, flow[i]); for (int i=pre[T]; i; i=pre[to[i^1]]) { flow[i] -= fl; flow[i^1] += fl; ret += 1ll * fl * w[i]; } return ret; } inline ll main() { ll ans = 0; while(spfa()) ans += mcf(); return ans; } } int main() { cin >> n >> K >> SS >> EE; ll sum = 0; for (int i=1; i<=n; ++i) { scanf("%d", &s[i]); sum += s[i]; } for (int i=1; i<=n; ++i) { scanf("%d", &e[i]); e[i] -= s[i]; } int mi = EE, mx = K - SS; int S0 = n+1; S = n+2; T = n+3; // [mi, mx] adde(S, S0, mx, 0); for (int i=1; i<=n; ++i) { if(i <= K) adde(S0, i, 1e9, 0); if(i+1 <= n) adde(i, i+1, mx-mi, 0); else adde(i, T, mx-mi, 0); if(i+K <= n) adde(i, i+K, 1, -e[i]); else adde(i, T, 1, -e[i]); id[i] = tot; } cout << sum - MCF::main() << endl; for (int i=1; i<=n; ++i) { if(flow[id[i]^1]) putchar('S'); else putchar('E'); } return 0; }