1. 程式人生 > >CF 704D Captain America 上下界網路流

CF 704D Captain America 上下界網路流

題目大意

給定N個點,每個點有一個座標(Xi,Yi)。現在要給每個點染成紅色或藍色,染成紅色的費用為r,染成藍色的費用為b。現在有M條約束,每條約束形如t,l,d
t=1:對於直線x=l上的點,紅色的點數與藍色的點數的差不能大於d
t=2:對於直線y=l上的點,紅色的點數與藍色的點數的差不能大於d
現在問最小的花費把所有點染色,並輸出方案。

N,M105
Xi,Yi109

解題思路

對於這種對x座標和y座標同時有約束的題,我們最直接的想法就是把垂直於x軸的直線和垂直為y軸的直線變成點放在兩側,把每一個點看作一條邊,連線代表Xi直線的點和代表Yi直線的點。題目給出的M條約束也只需解個方程來得到某條直線上某種顏色的點的最少數量和最大數量,那麼構完圖後,跑一下上下界網路流就可以了。由於是個二分圖,跑的很快!

程式

//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(); }