• bzoj2400 Spoj 839 Optimal Marks


    Description

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

    Input

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

    Output

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

    Sample Input

    3 2
    2
    -1
    0
    1 2
    2 3

    Sample Output

    2
    2

    HINT

    数据约定

      n<=500,m<=2000

     

    样例解释

        2结点的值定为0即可。

     
    很容易注意到异或的性质:把所有点权拆成二进制,那么每一位的取值是不会相互影响的。换句话说,第一个点拆成二进制,第一位不管取0还是1是不会影响到第二位的
    那么可以直接枚举二进制每一位,因为每一个点只能取0/1,显然二分图
    跑个最小割找出每个点的取值
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<deque>
    #include<set>
    #include<map>
    #include<ctime>
    #define LL long long
    #define inf 0x3ffffff
    #define S 0
    #define T 1001
    #define N 1010
    using namespace std;
    inline LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    struct edge{int to,next,v;}e[100*N];
    struct bian{int x,y;}p[2010];
    int n,m,cnt,ans;
    LL ans1,ans2;
    int head[N],q[N],h[N];
    int v[N];
    bool mrk[N],mrk2[N];
    inline void ins(int u,int v,int w)
    {
        e[++cnt].to=v;
        e[cnt].v=w;
        e[cnt].next=head[u];
        head[u]=cnt;
    }
    inline void insert(int u,int v,int w)
    {
        ins(u,v,w);
        ins(v,u,0);
    }
    inline bool bfs()
    {
        memset(h,-1,sizeof(h));
        int t=0,w=1;
        q[0]=S;h[S]=0;
        while (t!=w)
        {
            int now=q[t++];
            for(int i=head[now];i;i=e[i].next)
                if (e[i].v&&h[e[i].to]==-1)
                {
                    h[e[i].to]=h[now]+1;
                    q[w++]=e[i].to;
                }
        }
        return h[T]!=-1;
    }
    inline int dfs(int x,int f)
    {
        if (x==T||!f)return f;
        int w,used=0;
        for (int i=head[x];i;i=e[i].next)
            if (e[i].v&&h[e[i].to]==h[x]+1)
            {
                w=dfs(e[i].to,min(e[i].v,f-used));
                e[i].v-=w;
                e[i^1].v+=w;
                used+=w;
                if (f==used)return f;
            }
        if (!used)h[x]=-1;
        return used;
    }
    inline void dinic(){ans=0;while (bfs())ans+=dfs(S,inf);}
    inline void dfs2(int x)
    {
        mrk2[x]=1;
        for (int i=head[x];i;i=e[i].next)
            if(e[i^1].v&&!mrk2[e[i].to])dfs2(e[i].to);
    }
    int main()
    {
        n=read();m=read();
        for (int i=1;i<=n;i++)
        {
            int x=read();
            if (x<0)mrk[i]=1;else v[i]=x;
        }
        for (int i=1;i<=m;i++)
        {
            p[i].x=read();
            p[i].y=read();
        }
        for (int i=0;i<32;i++)
        {
            cnt=1;
            memset(head,0,sizeof(head));
            int kk=1<<i;
            for (int j=1;j<=n;j++)
                if (!mrk[j])
                {
                    if (v[j] & kk)insert(j,T,inf);
                    else insert(S,j,inf);
                }
            for (int j=1;j<=m;j++)
            {
                insert(p[j].x,p[j].y,1);
                insert(p[j].y,p[j].x,1);
            }
            dinic();
            ans1+=(LL)ans*kk;
            memset(mrk2,0,sizeof(mrk2));
            dfs2(T);
            for (int j=1;j<=n;j++)
                if (mrk2[j])ans2+=(LL)kk;
        }
        printf("%lld
    %lld
    ",ans1,ans2);
    }
    
    ——by zhber,转载请注明来源
  • 相关阅读:
    CAP原理、一致性模型、BASE理论和ACID特性
    MyBatis双数据源配置
    MySQL中间件Atlas安装及使用
    MySQL主从切换
    MySQL定时逻辑备份
    MySQL主从搭建
    zabbix监控nginx
    SVN Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。
    ie8下table的colspan属性与max-with属性的显示错乱问题
    MVC 自定义异常错误页面处理
  • 原文地址:https://www.cnblogs.com/zhber/p/4198303.html
Copyright © 2020-2023  润新知