图论(网络流):SPOJ OPTM - Optimal Marks
OPTM - Optimal Marks
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
- COGS上AC了,这里花46分钟买了个教训。
SPOJ:
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <queue>
- using namespace std;
- const int INF=;
- const int maxn=;
- const int maxm=;
- int cnt,fir[maxn],to[maxm],nxt[maxm],cap[maxm];
- void addedge(int a,int b,int c){
- nxt[++cnt]=fir[a];
- fir[a]=cnt;
- cap[cnt]=c;
- to[cnt]=b;
- }
- queue<int>q;
- int dis[maxn];
- bool BFS(int s,int t){
- dis[t]=;q.push(t);
- while(!q.empty()){
- int x=q.front();q.pop();
- for(int i=fir[x];i;i=nxt[i])
- if(!dis[to[i]]){
- dis[to[i]]=dis[x]+;
- q.push(to[i]);
- }
- }
- return dis[s];
- }
- int fron[maxn];
- int gap[maxn],path[maxn];
- int ISAP(int s,int t){
- if(!BFS(s,t))return ;
- for(int i=s;i<=t;i++)++gap[dis[i]];
- for(int i=s;i<=t;i++)fron[i]=fir[i];
- int p=s,ret=,f;
- while(dis[s]<=t+){
- if(p==t){
- f=INF;
- while(p!=s){
- f=min(f,cap[path[p]]);
- p=to[path[p]^];
- }
- ret+=f;p=t;
- while(p!=s){
- cap[path[p]]-=f;
- cap[path[p]^]+=f;
- p=to[path[p]^];
- }
- }
- int &ii=fron[p];
- for(;ii;ii=nxt[ii])
- if(cap[ii]&&dis[p]==dis[to[ii]]+)
- break;
- if(ii)
- path[p=to[ii]]=ii;
- else{
- if(--gap[dis[p]]==)break;
- int minn=t+;
- for(int i=fir[p];i;i=nxt[i])
- if(cap[i])minn=min(minn,dis[to[i]]);
- ++gap[dis[p]=minn+];ii=fir[p];
- if(p!=s)p=to[path[p]^];
- }
- }
- return ret;
- }
- void Init(){
- memset(fir,,sizeof(fir));
- memset(dis,,sizeof(dis));
- memset(gap,,sizeof(gap));
- cnt=;
- }
- int n,m,T;
- long long a[maxn],w[maxn];
- int E[maxm][],fa[maxn];
- int Find(int x){
- return fa[x]==x?x:fa[x]=Find(fa[x]);
- }
- int vis[maxn];
- void DFS(int x,int d){
- vis[x]=;a[x]|=d;
- for(int i=fir[x];i;i=nxt[i])
- if(cap[i]&&!vis[to[i]])
- DFS(to[i],d);
- }
- long long Solve(){
- int s=,t=n+;
- long long ret=;
- for(int k=;k<=;k++){
- Init();
- for(int i=;i<=n;i++)
- if(Find(i)==&&w[i]>=){
- if(w[i]>>k&){
- addedge(s,i,INF);
- addedge(i,s,);
- }
- else{
- addedge(i,t,INF);
- addedge(t,i,);
- }
- }
- for(int i=;i<=m;i++)
- if(Find(E[i][])==){
- addedge(E[i][],E[i][],);
- addedge(E[i][],E[i][],);
- }
- ret+=(1ll<<k)*ISAP(s,t);
- memset(vis,,sizeof(vis));
- DFS(s,1ll<<k);
- }
- return ret;
- }
- int main(){
- scanf("%d",&T);
- while(T--){
- scanf("%d%d",&n,&m);
- for(int i=;i<=m;i++)
- for(int j=;j<=;j++)
- scanf("%d",&E[i][j]);
- int u,v,k;
- scanf("%d",&k);
- memset(w,-,sizeof(w));
- memset(a,,sizeof(a));
- while(k--){
- scanf("%d",&u);
- scanf("%lld",&w[u]);
- }
- for(int i=;i<=n;i++)
- fa[i]=w[i]!=-?:i;
- for(int i=;i<=m;i++){
- u=Find(E[i][]);
- v=Find(E[i][]);
- if(u>v)swap(u,v);
- if(u!=v)fa[v]=u;
- }
- Solve();
- for(int i=;i<=n;i++)
- if(w[i]<)
- printf("%lld\n",a[i]);
- else
- printf("%lld\n",w[i]);
- }
- return ;
- }
图论(网络流):SPOJ OPTM - Optimal Marks的更多相关文章
- SPOJ OPTM - Optimal Marks
OPTM - Optimal Marks no tags You are given an undirected graph G(V, E). Each vertex has a mark whic ...
- 【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 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. ...
- SPOJ 839 OPTM - Optimal Marks (最小割)(权值扩大,灵活应用除和取模)
http://www.spoj.com/problems/OPTM/ 题意: 给出一张图,点有点权,边有边权 定义一条边的权值为其连接两点的异或和 定义一张图的权值为所有边的权值之和 已知部分点的点权 ...
- 【bzoj2400】Spoj 839 Optimal Marks 网络流最小割
题目描述 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其余的点的值由你 ...
- spoj 839 OPTM - Optimal Marks&&bzoj 2400【最小割】
因为是异或运算,所以考虑对每一位操作.对于所有已知mark的点,mark的当前位为1则连接(s,i,inf),否则连(i,t,inf),然后其他的边按照原图连(u,v,1),(v,u,1),跑最大流求 ...
- SPOJ839 OPTM - Optimal Marks
传送门 闵神讲网络流应用的例题,来水一水 要写出这道题,需要深入理解两个概念,异或和最小割. 异或具有相对独立性,所以我们把每一位拆开来看,即做大概$32$次最小割.然后累加即可. 然后是最小割把一张 ...
- BZOJ2400: Spoj 839 Optimal Marks
Description 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其 ...
- spoj 839 Optimal Marks(二进制位,最小割)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17875 [题意] 给定一个图,图的权定义为边的两端点相抑或值的 ...
随机推荐
- HTML5-36d嗨起^_^
如果一个前端不会写css那必然会被贻笑大方,利用html5新增的一些c3属性可以做许许多多炫酷的效果. 大家先看我写的一个小demo: http://zpf92.github.io/build/ 这个 ...
- js兼容各个浏览器的复制功能
看似简单的复制功能,用js做起来竟然遇到各种情况.刚开始在网上搜索到复制功能的几种实现方法,但是都不兼容.最后还是用的插件代码如下 html模板 <tr> <td>1</ ...
- HTML5 文件域+FileReader 分段读取文件(四)
一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...
- DataTable和List集合互转
/// <summary> /// 将集合转换成DataTable /// </summary> /// <param name="list"> ...
- eclipse alt + '/' not working.
searching for google,I observed that the 'content assist' shortcut key was take placed with 'ctrl + ...
- Asp.Net MVC安全更新MS14-059导致项目编译失败
微软最近一次安全更新MS14-059(链接:https://technet.microsoft.com/en-us/library/security/ms14-059)由于直接应用到了machine. ...
- Oracle数据导入导出imp/exp命令总结
racle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. 利用 ...
- Deep Learning 学习随记(四)自学习和非监督特征学习
接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...
- javascript——浅谈javascript模版(自定义)
/** * Created by Administrator on 15-1-19. */ function functionUtil() { } functionUtil = { //某个DOM节点 ...
- Java学习----对象间的继承
继承:子类可以使用父类非私有的成员变量和方法 public class Father { public String name; public String bloodType; private in ...