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 839 最小割应用)