spoj 839 最小割應用
http://www.spoj.com/problems/OPTM/
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
我的程式碼:
#include<cstdio>
#include <string.h>
#include<iostream>
using namespace std;
const int oo=1e9;
const int mm=11111;
const int mn=999;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0; i<node; ++i)head[i]=-1;
edge=0;
}
void addedge(int u,int v,int c)
{
ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
void addedge1(int u,int v,int c)
{
ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=c,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0; i<node; ++i)dis[i]=-1;
dis[q[r++]=src]=0;
for(l=0; l<r; ++l)
for(i=head[u=q[l]]; i>=0; i=next[i])
if(flow[i]&&dis[v=ver[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==dest)return 1;
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==dest)return exp;
for(int &i=work[u],v,tmp; i>=0; i=next[i])
if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
int Dinic_flow()
{
int i,ret=0,delta;
while(Dinic_bfs())
{
for(i=0; i<node; ++i)
work[i]=head[i];
while(delta=Dinic_dfs(src,oo))
ret+=delta;
}
return ret;
}
struct NOTE
{
int u,v;
} p[mm];
int n,m,k;
int a[mm][2];
int vis[mm];
int mark[mm];
void build(int cur)
{
prepare(n+2,0,n+1);
for(int i=0; i<m; i++)
addedge1(p[i].u,p[i].v,1);
for(int i=0; i<k; i++)
if(a[i][1]&1<<cur)
addedge(src,a[i][0],oo);
else
addedge(a[i][0],dest,oo);
}
void dfs(int cur,int k)
{
vis[cur]=1;
mark[cur]|=1<<k;
for(int i=head[cur]; i!=-1; i=next[i])
if(!vis[ver[i]]&&flow[i])
dfs(ver[i],k);
}
void solve()
{
long long ans=0;
memset(mark,0,sizeof(mark));
for(int i=0; i<31; i++)
{
build(i);
ans+=Dinic_flow()*(1ll<<i);
memset(vis,0,sizeof(vis));
dfs(0,i);
}
for(int i=1; i<=n; i++)
printf("%d\n",mark[i]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
scanf("%d%d",&p[i].u,&p[i].v);
scanf("%d",&k);
for(int i=0; i<k; i++)
scanf("%d%d",&a[i][0],&a[i][1]);
solve();
}
return 0;
}
相關文章
- SPOJ - OPTM Optimal Marks(進位制拆分+最小割)
- 網路流-最小割
- 網路流(最大流,最小割)
- bzoj2127: happiness(最小割)APP
- matlab練習程式(最大流/最小割)Matlab
- 偷寶石(最大流轉化最小割)
- 1.1.3.3 最小割之最小權覆蓋集、最大權獨立集
- POJ 3308 Paratroopers 最小割、最大流OOP
- 網路流最大流、最小割學習筆記筆記
- 論文導讀 | 最小屬性割RDF資料劃分
- 839相似字串字串
- CS 839: FOUNDATION MODELS
- 2014多校聯合第十場A題||hdu 4971 最小割定理在最大權閉合圖上的應用
- 基於flask的最小的應用Flask
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割演算法)演算法
- POJ 2914-Minimum Cut(Stoer_Wagner最小割演算法)演算法
- SPOJ GSS3 (動態dp)S3
- SPOJ COT3 - Combat on a treeBAT
- 無向圖最小割問題取得新突破,谷歌研究獲SODA 2024最佳論文獎谷歌
- SPOJ TTM To the moon(主席樹+區間操作)
- 嗅探器(割點)
- 割繩子html5HTML
- 最小化醫療保健中應用程式威脅風險的幾種方法
- WWDC最小參會者年齡僅9歲:已開發兩款應用
- 樹鏈剖分模板+入門題 SPOJ - QTREEQT
- 網路中最小費用最大流
- SPOJ DQUERY (離線數狀陣列||線上主席樹)陣列
- 【筆記/模板】割點和橋筆記
- 用 Node 搭建最小實現腳手架
- c++閹割版binder實現C++
- 用selenium開啟網頁的最小模板網頁
- oracle重要系統割接準備工作Oracle
- 一次被割韭菜的覆盤
- 用於靜態網站的最小Docker映象 - lipanski網站Docker
- POJ 2195 Going Home 最小費用最大流Go
- 37 Signals的實用最小主義實踐
- POJ 2195 Going Home (最小費用最大流)Go