人生的第一黑题+ cf3100
给定每个点的坐标,可以由丰富的二分图知识想出,将行和列分别拆成左部点和右部点,则对应的坐标将表示为相应的行列点间存在连线。由于
x
i
x_i
xi 和
y
i
y_i
yi 的范围很大,需要进行离散化处理,同时记录该点是左部点还是右部点,方便之后与源汇连接。
可以假设涂成红色需要的花费 r r r < 涂成蓝色需要的花费 b b b,问题即可转化为在限制条件下,能够将点涂成红色的最大值,即最大流。
对于作用于同一直线上的限制,将采用该直线上最严格的(即最小的 d d d)作为最终限制。假设该直线上存在的点的数量为 n u m num num,最严格限制为 l i m i t limit limit,则该行内允许的红色点数目为 [ n u m − l i m i t + 1 2 , n u m + l i m i t 2 ] [\frac{num-limit+1}{2},\frac{num+limit}{2}] [2num−limit+1,2num+limit] 。如果是左部点,由源点 s 1 s1 s1 连边,否则向汇点 t 1 t1 t1 连边。
对于每个坐标,左部点向右部点连上下界为 [ 0 , 1 ] [0,1] [0,1] 的边,跑有源汇上下界最大流。对于有流量的边,说明该点涂成红色,否则涂成蓝色。如果花费 r > b r>b r>b,需要颠倒。
本题中,注意多特判下界大于上界时也是无解的情况。
参考代码
#include <bits/stdc++.h>
#define itn int
#define int long long
#define endl "\n"
#define PII pair<int, int>
using namespace std;
const int N = 1e6 + 10;
// const int M = 4e5 + 10;
const itn inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
// const int mod = 998244353;
struct Edge {
int from, to, cap, flow;
Edge(int f, int t, int c, int fl) {
from = f;
to = t;
cap = c;
flow = fl;
}
};
struct Dinic {
int n, m, s, t; //结点数,边数(包括反向弧),源点编号和汇点编号
vector<Edge> edges; //边表。edge[e]和edge[e^1]互为反向弧
vector<int> G[N]; //邻接表,G[i][j]表示节点i和第j条边在e数组中的序号
bool vis[N]; // BFS使用
int d[N]; //从起点到i的距离
int cur[N]; //当前弧下标
void clear_all(int n) {
for (int i = 0; i < n; i++)
G[i].clear();
edges.clear();
}
void clear_flow() {
int len = edges.size();
for (int i = 0; i < len; i++)
edges[i].flow = 0;
}
void add_edge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
int len = G[x].size();
for (int i = 0; i < len; i++) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f, len = G[x].size();
for (int& i = cur[x]; i < len; i++) {
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] &&
(f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0)
break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s;
this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}
int mincut() { // call this after maxflow
int ans = 0;
int len = edges.size();
for (int i = 0; i < len; i++) {
Edge& e = edges[i];
if (vis[e.from] && !vis[e.to] && e.cap > 0)
ans++;
}
return ans;
}
void reduce() {
int len = edges.size();
for (int i = 0; i < len; i++)
edges[i].cap -= edges[i].flow;
}
} dinic;
int n, m, s1, s2, t1, t2, red, blue;
vector<PII> h;
int in[N];
int limit[N]; //每行每列的最小限制
map<int, int> idx, idy; //数据范围1e9 离散化
int sum[N]; //每行每列上有几个数
int ri[N]; //是否为右部点
void ins(int u, int v, int l, int r) {
dinic.add_edge(u, v, r - l);
in[u] -= l;
in[v] += l;
}
void solve() {
cin >> n >> m >> red >> blue;
int fk = 0, cnt = 0;
if (red > blue)
swap(red, blue), fk = 1;
for (itn i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (!idx[x])
idx[x] = ++cnt, limit[cnt] = inf;
sum[idx[x]]++;
if (!idy[y])
idy[y] = ++cnt, ri[cnt] = 1, limit[cnt] = inf;
sum[idy[y]]++;
dinic.add_edge(idx[x], idy[y], 1);
}
for (int i = 1; i <= m; i++) {
int op, l, d;
cin >> op >> l >> d;
if (op == 1) {
l = idx[l];
if (!l)
continue;
limit[l] = min(limit[l], d);
} else {
l = idy[l];
if (!l)
continue;
limit[l] = min(limit[l], d);
}
}
s1 = cnt + 1, t1 = cnt + 2;
s2 = cnt + 3, t2 = cnt + 4;
for (int i = 1; i <= cnt; i++) {
int num = sum[i], lim = min(limit[i], num); //保证下界不小于0
int l = (num - lim + 1) / 2, r = (num + lim) / 2;
if (r < l) { //下界比上界大
cout << "-1" << endl;
return;
}
if (ri[i] == 0) { //左部点
ins(s1, i, l, r);
} else //右部点
ins(i, t1, l, r);
}
int sumn = 0;
for (int i = 1; i <= t1; i++) {
if (in[i] < 0)
dinic.add_edge(i, t2, -in[i]); //流入太多 给汇点
else
dinic.add_edge(s2, i, in[i]), sumn += in[i]; //流出太多 源点补充
}
dinic.add_edge(t1, s1, inf);
int len = dinic.edges.size();
itn flow1 = dinic.maxflow(s2, t2);
// cout << flow1 << endl;
if (flow1 != sumn) {
cout << "-1" << endl;
return;
}
dinic.reduce();
dinic.clear_flow();
flow1 = dinic.edges[len - 1].cap;
dinic.edges[len - 2].cap = dinic.edges[len - 1].cap = 0;
//删边
int flow2 = dinic.maxflow(s1, t1);
int flow = flow1 + flow2;
cout << flow * red + (n - flow) * blue << endl;
dinic.reduce();
dinic.clear_flow();
for (int i = 0; i < 2 * n; i += 2) {
if (dinic.edges[i].cap == 0) { //有流量
if (fk)
cout << "b";
else
cout << "r";
} else {
if (fk)
cout << "r";
else
cout << "b";
}
}
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(12);
// init();
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}