839. Optimal Marks - SPOJ
You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range [0..231 – 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.
Input
The first line of the input data contains integer T (1 ≤ T ≤ 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 <= 500, 0 <= M <= 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.
Output
For each testcase you should print N lines integer the output. The Kth 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.
Example
Input:
1
3 2
1 2
2 3
2
1 5
3 100 Output:
5
4
100
详情请见胡伯涛的论文
经典最小割模型
首先我们分位处理,因为每一位都是独立的,互不影响,然后每个点就变成0或1
再想一下割集的定义,割集把一个图分成了两块,而割集就是这两块之间的连边(好吧,这是我乱编的.....)
xor正好是值不同才会贡献答案,于是建立最小割模型
设一个源s,向确定是1的点连容量为inf的边(因为他不能贡献答案,不能让他成为最小割)
设一个汇t,从确定是0的点向汇连容量为inf的边,理由同上
其他不确定的点就和所有的点(除了s和t)连容量为1的边(双向都要连)
然后跑最大流,也就是最小割
然后我们看这个割集,他把图分成了两部分,最后与s在一起的就是最后为1的(即在残留网络上可以从s走到他),不与s在一起的就是最后为0的,因为是最小割,所以是代价最小的
然后我们要让总和最小,其实我们已经做到了
与s在一起的必须是1,可以画图看一看,不与s在一起的不一定选1,为了和最小,就选0
每一位做一次,最后输出答案就行了
const
maxn=;
inf=;
var
t,si,ti,n,m,step,time:longint;
flag:array[..maxn]of boolean;
tu:array[..maxn,..maxn]of boolean;
map:array[..maxn,..maxn]of longint;
a,dis,vh,his,pre,vis:array[..maxn]of longint; procedure sap;
var
i,j,min,aug:longint;
flag:boolean;
begin
fillchar(dis,sizeof(dis),);
fillchar(vh,sizeof(vh),);
i:=si;
vh[]:=n+;
aug:=inf;
while dis[i]<n+ do
begin
his[i]:=aug;
flag:=false;
for j:= to ti do
if tu[i,j] then
if (map[i,j]>) and (dis[i]=dis[j]+) then
begin
if aug>map[i,j] then aug:=map[i,j];
flag:=true;
pre[j]:=i;
i:=j;
if i=ti then
begin
while i<>si do
begin
inc(map[i,pre[i]]);
dec(map[pre[i],i]);
i:=pre[i];
end;
aug:=inf;
end;
break;
end;
if flag then continue;
min:=n+;
for j:= to ti do
if tu[i,j] then
if (map[i,j]>) and (dis[j]<min) then min:=dis[j];
dec(vh[dis[i]]);
if vh[dis[i]]= then break;
dis[i]:=min+;
inc(vh[dis[i]]);
if i<>si then
begin
i:=pre[i];
aug:=his[i];
end;
end;
end; procedure dfs(x:longint);
var
i:longint;
begin
vis[x]:=time;
if flag[x]=false then inc(a[x],<<step);
for i:= to ti do
if vis[i]<>time then
if tu[x,i] then
if map[x,i]> then dfs(i);
end; procedure main;
var
j,k,m,x,y:longint;
begin
fillchar(flag,sizeof(flag),false);
fillchar(tu,sizeof(tu),false);
fillchar(a,sizeof(a),);
read(n,m);
si:=;
ti:=n+;
for j:= to m do
begin
read(x,y);
tu[y,x]:=true;
tu[x,y]:=true;
end;
for j:= to n do
begin
tu[si,j]:=true;
tu[j,ti]:=true;
end;
read(m);
for j:= to m do
begin
read(x,y);
flag[x]:=true;
a[x]:=y;
end;
for step:= to do
begin
fillchar(map,sizeof(map),);
for j:= to n do
if flag[j] then
if a[j] and (<<step)= then
begin
map[j,ti]:=inf;
for k:= to n do
if flag[k]=false then map[k,j]:=;
end
else
begin
map[si,j]:=inf;
for k:= to n do
if flag[k]=false then map[j,k]:=;
end
else
for k:=j+ to n do
if flag[k]=false then
begin
map[j,k]:=;
map[k,j]:=;
end;
sap;
inc(time);
dfs();
end;
for j:= to n do
writeln(a[j]);
end; begin
read(t);
while t> do
begin
main;
dec(t);
end;
end.
839. Optimal Marks - SPOJ的更多相关文章
- 【bzoj2400】Spoj 839 Optimal Marks 按位最大流
Spoj 839 Optimal Marks Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 908 Solved: 347[Submit][Stat ...
- 【BZOJ2400】Spoj 839 Optimal Marks 最小割
[BZOJ2400]Spoj 839 Optimal Marks Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. ...
- BZOJ2400: Spoj 839 Optimal Marks
Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其 ...
- spoj 839 Optimal Marks(二进制位,最小割)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17875 [题意] 给定一个图,图的权定义为边的两端点相抑或值的 ...
- Optimal Marks SPOJ 839
这题远超其他题非常靠近最小割的实际意义: 割边<=>付出代价<=>决定让两个点的值不相同,边权增加 最小割<=>点的值与s一个阵营的与s相同,与t一个阵营的与t相同 ...
- SPOJ 839 Optimal Marks(最小割的应用)
https://vjudge.net/problem/SPOJ-OPTM 题意: 给出一个无向图G,每个点 v 以一个有界非负整数 lv 作为标号,每条边e=(u,v)的权w定义为该边的两个端点的标号 ...
- 【bzoj2400】Spoj 839 Optimal Marks 网络流最小割
题目描述 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其余的点的值由你 ...
- BZOJ 2400: Spoj 839 Optimal Marks (按位最小割)
题面 一个无向图,一些点有固定权值,另外的点权值由你来定. 边的值为两点的异或值,一个无向图的值定义为所有边的值之和. 求无向图的最小值 分析 每一位都互不干扰,按位处理. 用最小割算最小值 保留原图 ...
- Optimal Marks SPOJ - OPTM (按位枚举-最小割)
题意:给一张无向图,每个点有其点权,边(i,j)的cost是\(val_i\ XOR \ val_j\).现在只给出K个点的权值,求如何安排其余的点,使总花费最小. 分析:题目保证权值不超过32位整型 ...
随机推荐
- 关于CSS的一些总结
通过对CSS基础一天的学习以及练习,觉得自己以前还是蛮无知的,一直以为CSS样式是别人写好的,自己只需要像导包一样拿过来用就可以.直到自己认真学了之后才直到是什么样的.自己如果不去敲代码感觉永远都学不 ...
- asp网站发布步骤总结
1.在VS2012中打开索要发布的网站,初始页可重命名为index.html或default.apx. 2.点击 生成>生成“网站”,然后“发布网站”. 3.进行发布设置: (1 配置文件 ( ...
- MYfirst
终于有了自己的博客了,啊哈哈!
- SSRS 传多值参数问题
SSRS报表参数是可单值或者多值.之前有个小伙伴问我,如果要传多值怎么传.然后我试了在各个参数之间,放换行符放逗号分号等都未能解决问题,最后想明白这个参数接受的应该是非字符类型,很大可能是数组,然后我 ...
- OC3_歌词解析
// // LrcManager.h // OC3_歌词解析 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zhan ...
- 从0开始学习react(三)
这次我们来讲解第三节知识,考虑了下,先不去讲什么理论了,毕竟网上一搜一大堆,而且理论真心看不太懂啊!!! 今天我们就直接上实例喽! 大家HIGH起来!!!(想了好久,还是没舍得删这句话) 1.根据下图 ...
- Js 中的this
关于this This代表"当前"对象 谁调用就代表谁 行内绑定 不带参数 行内绑定 带参数 this 当前对象 Object HTMLInputElement 动态绑定 要获取自 ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [zz] 安装PostGIS(Linux篇)
0.安装PostgreSQL数据库 参考安装PostgreSQL数据库(Linux篇). 1.安装proj4 #tar zxvf proj-4.8.0.tar.gz#cd proj-4.8.0#./c ...
- Git初始化与上传
一: 现在git上Create个repository 二:进入要长传的工程目录打开git bash git initgit statusgit add .//add .的时候文件不要被占用. git ...