图论(网络流):SPOJ OPTM - Optimal Marks
2016-07-04 20:52
344 查看
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=1000000000; const int maxn=1010; const int maxm=30010; 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]=1;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]+1; 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 0; 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=0,f; while(dis[s]<=t+10){ if(p==t){ f=INF; while(p!=s){ f=min(f,cap[path[p]]); p=to[path[p]^1]; } ret+=f;p=t; while(p!=s){ cap[path[p]]-=f; cap[path[p]^1]+=f; p=to[path[p]^1]; } } int &ii=fron[p]; for(;ii;ii=nxt[ii]) if(cap[ii]&&dis[p]==dis[to[ii]]+1) break; if(ii) path[p=to[ii]]=ii; else{ if(--gap[dis[p]]==0)break; int minn=t+1; for(int i=fir[p];i;i=nxt[i]) if(cap[i])minn=min(minn,dis[to[i]]); ++gap[dis[p]=minn+1];ii=fir[p]; if(p!=s)p=to[path[p]^1]; } } return ret; } void Init(){ memset(fir,0,sizeof(fir)); memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); cnt=1; } int n,m,T; long long a[maxn],w[maxn]; int E[maxm][2],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]=1;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=0,t=n+1; long long ret=0; for(int k=0;k<=30;k++){ Init(); for(int i=1;i<=n;i++) if(Find(i)==0&&w[i]>=0){ if(w[i]>>k&1){ addedge(s,i,INF); addedge(i,s,0); } else{ addedge(i,t,INF); addedge(t,i,0); } } for(int i=1;i<=m;i++) if(Find(E[i][0])==0){ addedge(E[i][0],E[i][1],1); addedge(E[i][1],E[i][0],1); } ret+=(1ll<<k)*ISAP(s,t); memset(vis,0,sizeof(vis)); DFS(s,1ll<<k); } return ret; } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) for(int j=0;j<=1;j++) scanf("%d",&E[i][j]); int u,v,k; scanf("%d",&k); memset(w,-1,sizeof(w)); memset(a,0,sizeof(a)); while(k--){ scanf("%d",&u); scanf("%lld",&w[u]); } for(int i=1;i<=n;i++) fa[i]=w[i]!=-1?0:i; for(int i=1;i<=m;i++){ u=Find(E[i][0]); v=Find(E[i][1]); if(u>v)swap(u,v); if(u!=v)fa[v]=u; } Solve(); for(int i=1;i<=n;i++) if(w[i]<0) printf("%lld\n",a[i]); else printf("%lld\n",w[i]); } return 0; }
相关文章推荐
- OkHttp
- Live555学习笔记(三)—— RTSP命令处理及RTP,RTCP服务建立
- 《HTTP权威指南》——认证机制
- HTTP请求报文和HTTP响应报文
- 使用HTTP头去绕过WAF
- HTML5+规范:XMLHttpRequest(管理网络请求)
- HTML5+规范:Uploader(管理网络上传任务)
- 通信网络
- 用JS做了个手机滑动相册 作者: 夜麟 链接:http://www.imooc.com/article/9483 来源:慕课网
- 计算机网络(2)-----ARP协议
- OpenStack网络实战系列一:通过Openvswitch实践了解交换机的基本概念和操作
- HTTP Status 405 - HTTP method POST/GET is not supported by this URLyi
- Http请求和响应
- 使用http协议访问遍历访问下载网站的所有图片
- 计算机网络(1)-----网络层IP协议概述
- HTTP协议详解
- Swift---网络判断封装(可直接使用)
- openstack学习笔记二 网络设置基础
- 虚拟机Linux配置网络(桥接和共享两种方法)
- tcp/ip,http,socket mysql底层技术原理