BZOJ 2400: Spoj 839 Optimal Marks 网络流

来源:互联网 发布:电脑office软件下载 编辑:程序博客网 时间:2024/06/12 18:49

Description

定义无向图中的一条边的值为:这条边连接的两个点的值的异或值。
定义一个无向图的值为:这个无向图所有边的值的和。
给你一个有n个结点m条边的无向图。其中的一些点的值是给定的,而其余的点的值由你决定(但要求均为非负数),使得这个无向图的值最小。在无向图的值最小的前提下,使得无向图中所有点的值的和最小。

Input

第一行,两个数n,m,表示图的点数和边数。
接下来n行,每行一个数,按编号给出每个点的值(若为负数则表示这个点的值由你决定,值的绝对值大小不超过10^9)。
接下来m行,每行二个数a,b,表示编号为a与b的两点间连一条边。(保证无重边与自环。)

Output

第一行,一个数,表示无向图的值。第二行,一个数,表示无向图中所有点的值的和。

Sample Input

3 22-101 22 3

Sample Output

22

HINT

数据约定

n<=500,m<=2000

样例解释

2结点的值定为0即可。

题解

二进制的题我们不难想到把每一位分开讨论,如果不考虑第二问,两个点选择不一样就要有一个权值,就变成了一个很裸的最小割模型,加上第二问,我们只要把边权乘上一个很大的值就行了,最后ans/INF 为第一问答案,ans%INF 为第二问答案

#include<cstdio>#include<cstdlib>#include<ctime>#include<cmath>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<iomanip>using namespace std;#define INF 0x3f3f3f3fint val[501];int l[3001];int r[3001];struct bian{    int l,r,f;}a[20000];int fir[510];int nex[20000];int d[510];int S=0,T=509;int tot=1;void _add_edge(int l,int r,int f){    a[++tot].l=l;    a[tot].r=r;    a[tot].f=f;    nex[tot]=fir[l];    fir[l]=tot;}void add_edge(int l,int r,int f){    _add_edge(l,r,f);    _add_edge(r,l,0);}bool bfs(){    static int dui[510];    int s=1,t=1;    memset(d,-1,sizeof(d));    dui[t++]=S;    d[S]=0;    while(s<t)    {        int u=dui[s++];        for(int o=fir[u];o;o=nex[o])        {            if(!a[o].f) continue;            if(d[a[o].r]!=-1) continue;            d[a[o].r]=d[u]+1;            dui[t++]=a[o].r;            if(a[o].r==T) return true;        }    }    return false;}int dinic(int u,int flow){    if(u==T) return flow;    int left=flow;    for(int o=fir[u];o && left;o=nex[o])    {        if(!a[o].f || d[a[o].r]!=d[u]+1) continue;        int temp=dinic(a[o].r,min(a[o].f,left));        a[o].f-=temp;        a[o^1].f+=temp;        left-=temp;        if(!temp) d[a[o].r]=-1;     }    return flow-left;}int main(){    int n,m;    scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++) scanf("%d",&val[i]);    for(int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]);    long long ans1=0,ans2=0;    for(int i=0;i<=30;i++)    {        tot=1;        memset(fir,0,sizeof(fir));        for(int j=1;j<=n;j++)        {            if(val[j]<0) add_edge(S,j,1);            else if(val[j]&(1<<i))            {                add_edge(j,T,INF);                add_edge(S,j,1);            }            else add_edge(S,j,INF);        }        for(int j=1;j<=m;j++)        {            add_edge(l[j],r[j],10000);            add_edge(r[j],l[j],10000);        }        int ans=0;        while(bfs()) ans+=dinic(S,INF);        ans1+=1ll*(ans/10000)*(1<<i);        ans2+=1ll*(ans%10000)*(1<<i);    }    cout<<ans1<<endl<<ans2<<endl;    return 0;}