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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值