[SPOJ839]Optimal Marks
试题描述
You are given an undirected graph (G(V, E)). Each vertex has a mark which is an integer from the range ([0..2^{31} - 1]). Different vertexes may have the same mark.
For an edge ((u, v)), we define (Cost(u, v) = mark[u] xor mark[v]).
Now we know the marks of some certain nodes. You have to determine the marks of other nodes so that the total cost of edges is as small as possible.
给你一个无向图,有些点权值固定,你需要个剩下的点确定权值使得所有边的权值之和最小。边权定义为它连接的两个点的点权异或。
输入
The first line of the input data contains integer (T) ((1 le T le 10)) - the number of testcases. Then the descriptions of (T) testcases follow.
First line of each testcase contains (2) integers (N) and (M) ((0 < N le 500, 0 le M le 3000)). (N) is the number of vertexes and (M) is the number of edges. Then (M) lines describing edges follow, each of them contains two integers (u, v) representing an edge connecting (u) and (v).
Then an integer (K), representing the number of nodes whose mark is known. The next (K) lines contain (2) integers (u) and (p) each, meaning that node (u) has a mark (p). It’s guaranteed that nodes won’t duplicate in this part.
输出
For each testcase you should print (N) lines integer the output. The (K)th line contains an integer number representing the mark of node (K). If there are several solutions, you have to output the one which minimize the sum of marks. If there are several solutions, just output any of them.
输入示例
1
3 2
1 2
2 3
2
1 5
3 100
输出示例
5
4
100
数据规模及约定
见“输入”
题解
异或、求和,每一位都互不影响,套路做法就是按位处理。
对于某一位,每个点的点权只有 (0/1) 两种情况,自然想到 (0) 的点属于 (S) 割,(1) 的点属于 (T) 割,然后把原图复制上去,无向边边权为 (1),跑最小割。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i++)
#define dwn(i, s, t) for(int i = (s); i >= (t); i--)
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
#define maxn 510
#define maxm 7010
#define oo 2147483647
struct Edge {
int from, to, flow;
Edge() {}
Edge(int _1, int _2, int _3): from(_1), to(_2), flow(_3) {}
};
struct Dinic {
int n, m, s, t, head[maxn], nxt[maxm];
Edge es[maxm];
int vis[maxn], Q[maxn], hd, tl;
int cur[maxn];
bool tcut[maxn];
void init() {
m = 0; memset(head, -1, sizeof(head));
return ;
}
void setn(int _) {
n = _;
return ;
}
void AddEdge(int a, int b, int c) {
es[m] = Edge(a, b, c); nxt[m] = head[a]; head[a] = m++;
return ;
}
bool BFS() {
memset(vis, 0, sizeof(vis));
hd = tl = 0; Q[++tl] = t;
vis[t] = 1;
while(hd < tl) {
int u = Q[++hd];
for(int i = head[u]; i != -1; i = nxt[i]) {
Edge& e = es[i^1];
if(!vis[e.from] && e.flow) {
vis[e.from] = vis[u] + 1;
Q[++tl] = e.from;
}
}
}
return vis[s] > 0;
}
int DFS(int u, int a) {
if(u == t || !a) return a;
int flow = 0, f;
for(int& i = cur[u]; i != -1; i = nxt[i]) {
Edge& e = es[i];
if(vis[e.to] == vis[u] - 1 && (f = DFS(e.to, min(a, e.flow)))) {
flow += f; a -= f;
e.flow -= f; es[i^1].flow += f;
if(!a) return flow;
}
}
return flow;
}
int MaxFlow(int _s, int _t) {
s = _s; t = _t;
int flow = 0;
while(BFS()) {
rep(i, 1, n) cur[i] = head[i];
flow += DFS(s, oo);
}
return flow;
}
void dfs(int u) {
if(tcut[u]) return ;
tcut[u] = 1;
for(int i = head[u]; i != -1; i = nxt[i]) {
Edge& e = es[i^1];
if(e.flow) dfs(e.from);
}
return ;
}
} sol;
#define pii pair <int, int>
#define x first
#define y second
#define mp(x, y) make_pair(x, y)
pii es[maxm];
int n, m, val[maxn], getv[maxn];
void solve(int bit) {
int s = n + 1, t = n + 2;
sol.init(); sol.setn(t);
rep(i, 1, m) sol.AddEdge(es[i].x, es[i].y, 1), sol.AddEdge(es[i].y, es[i].x, 1);
rep(i, 1, n) if(val[i] >= 0) {
if(val[i] >> bit & 1) sol.AddEdge(i, t, oo), sol.AddEdge(t, i, 0);
else sol.AddEdge(s, i, oo), sol.AddEdge(i, s, 0);
}
sol.MaxFlow(s, t);
memset(sol.tcut, 0, sizeof(sol.tcut));
sol.dfs(t);
rep(i, 1, n) if(val[i] < 0)
getv[i] |= sol.tcut[i] << bit;
return ;
}
int main() {
int T = read();
while(T--) {
n = read(); m = read();
memset(val, -1, sizeof(val));
rep(i, 1, m) {
int a = read(), b = read();
es[i] = mp(a, b);
}
int k = read();
rep(i, 1, k) {
int t = read();
val[t] = read();
}
memset(getv, 0, sizeof(getv));
rep(i, 0, 31) solve(i);
rep(i, 1, n) if(val[i] >= 0) printf("%d
", val[i]); else printf("%d
", getv[i]);
}
return 0;
}
大概这题数据范围有问题,二进制位从 (0) 到 (30) 就 WA,到 (31) 就 A 了。