CF 704D Captain America 上下界網路流
阿新 • • 發佈:2019-01-03
題目大意
給定
現在問最小的花費把所有點染色,並輸出方案。
解題思路
對於這種對
程式
//YxuanwKeith
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 10;
const int Inf = 1e9 + 7;
struct Coor {
int x, y;
} P[MAXN];
map<int,int> HashX, HashY;
int N, M, r, b, CntX, CntY, ConX[MAXN], ConY[MAXN], NumX[MAXN], NumY[MAXN];
int S, T, SS, TT, Count[MAXN * 2], High[MAXN * 2];
int tot, Last[MAXN * 2], Go[MAXN * 10], Next[MAXN * 10], Len[MAXN * 10];
bool Flag[MAXN];
void Link(int u, int v, int len) {
Next[++ tot] = Last[u], Last[u] = tot, Go[tot] = v, Len[tot] = len;
}
void Open(int u, int v, int len) {
Link(u, v, len), Link(v, u, 0 );
}
bool Open(int u, int v, int Min, int Max) {
if (Min > Max) return 0;
Open(u, v, Max - Min);
Open(u, TT, Min), Open(SS, v, Min);
return 1;
}
int Dfs(int Now, int flow, int From, int to) {
if (Now == to) return flow;
int Use = 0;
for (int p = Last[Now]; p; p = Next[p]) {
int v = Go[p];
if (!Flag[v] && Len[p] > 0 && High[v] + 1 == High[Now]) {
int t = Dfs(v, min(Len[p], flow - Use), From, to);
Len[p] -= t, Len[p ^ 1] += t, Use += t;
if (Use == flow) return flow;
}
}
if (!(-- Count[High[Now]])) High[From] = TT + 1;
Count[++ High[Now]] ++;
return Use;
}
int Flow_Go(int From, int to) {
memset(Count, 0, sizeof Count);
memset(High, 0, sizeof High);
Count[0] = to + 1;
int Ans = 0;
while (High[From] <= to) Ans += Dfs(From, Inf, From, to);
return Ans;
}
void Solve() {
Open(T, S, Inf);
Flow_Go(SS, TT);
for (int p = Last[SS]; p; p = Next[p])
if (Len[p]) {
printf("-1\n");
return;
}
int Ans = Len[Last[S]];
Flag[SS] = Flag[TT] = 1;
Last[S] = Next[Last[S]], Last[T] = Next[Last[T]];
Ans += Flow_Go(S, T);
char c1 = 'r', c2 = 'b';
if (r > b) swap(r, b), swap(c1, c2);
printf("%lld\n", 1ll * Ans * r + 1ll * (N - Ans) * b);
for (int i = 1; i <= N; i ++)
if (Len[i * 2 + 1] == 1) printf("%c", c1); else printf("%c", c2);
}
void Prepare() {
S = 0, T = CntX + CntY + 1, SS = T + 1, TT = SS + 1;
for (int i = 1; i <= CntX; i ++) ConX[i] = NumX[i];
for (int i = 1; i <= CntY; i ++) ConY[i] = NumY[i];
for (int i = 1; i <= M; i ++) {
int t, l, d;
scanf("%d%d%d", &t, &l, &d);
if (t == 1) {
int x = HashX[l];
if (!x) continue;
ConX[x] = min(ConX[x], d);
} else {
int y = HashY[l];
if (!y) continue;
ConY[y] = min(ConY[y], d);
}
}
tot = 1;
for (int i = 1; i <= N; i ++) Open(P[i].x, P[i].y + CntX, 1);
for (int i = 1; i <= CntX; i ++) {
if (!Open(S, i, max(0, (NumX[i] - ConX[i] + 1) / 2), min(NumX[i], (NumX[i] + ConX[i]) / 2))) {
printf("-1\n");
return;
}
}
for (int i = 1; i <= CntY; i ++) {
if (!Open(i + CntX, T, max(0, (NumY[i] - ConY[i] + 1) / 2), min(NumY[i], (NumY[i] + ConY[i]) / 2))) {
printf("-1\n");
return;
}
}
Solve();
}
int main() {
scanf("%d%d", &N, &M);
scanf("%d%d", &r, &b);
for (int i = 1; i <= N; i ++) {
int x, y;
scanf("%d%d", &x, &y);
if (!HashX[x]) HashX[x] = ++ CntX;
if (!HashY[y]) HashY[y] = ++ CntY;
P[i].x = HashX[x], P[i].y = HashY[y];
NumX[P[i].x] ++, NumY[P[i].y] ++;
}
Prepare();
}