zoukankan      html  css  js  c++  java
  • [bzoj1934][Shoi2007]Vote 善意的投票

    来自FallDream的博客,未经允许,请勿转载,谢谢。


    幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的人数。 我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?

    n<=300

    每个小朋友如果想睡觉那么从S向他连边,如果想修仙从它向T连边,然后朋友之间互相连边。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define S 0
    #define T 301
    #define INF 2000000000
    using namespace std;
    inline int read()
    {
        int 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;
    }
    
    int n,m,q[T+5],top=0,head[T+5],c[T+5],d[T+5],cnt=1,ans=0;
    struct edge{int to,next,w;}e[T*T*10+5];
    
    void ins(int f,int t,int w)
    {
        e[++cnt]=(edge){t,head[f],w};head[f]=cnt;
        e[++cnt]=(edge){f,head[t],0};head[t]=cnt;
    }
    
    int dfs(int x,int f)
    {
        if(x==T) return f;
        int used=0;
        for(int&i=c[x];i;i=e[i].next)
            if(e[i].w&&d[e[i].to]==d[x]+1)
            {
                int w=dfs(e[i].to,min(f-used,e[i].w));
                used+=w;e[i].w-=w;e[i^1].w+=w;
                if(f==used) return f;    
            }
        return d[x]=-1,used;
    }
    
    bool bfs()
    {
        memset(d,0,sizeof(d));int i,j;
        for(d[q[top=i=1]=S]=1;i<=top;++i)
            for(int j=c[q[i]]=head[q[i]];j;j=e[j].next)
                if(e[j].w&&!d[e[j].to])
                    d[q[++top]=e[j].to]=d[q[i]]+1;
        return d[T];
    }
    
    int main()
    {
        n=read();m=read();
        for(int i=1;i<=n;i++) read()?ins(S,i,1):ins(i,T,1);
        for(int i=1;i<=m;i++)
        {
            int x=read(),y=read();
            ins(x,y,1);ins(y,x,1);    
        }
        while(bfs()) ans+=dfs(S,INF);
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    PHP实现微信小程序人脸识别刷脸登录功能
    thinkphp3.2.3中设置路由,优化url
    ThinkPHP URL 路由简介
    在 Linux 下搭建 Git 服务器
    图解 Android 广播机制 狼人:
    手机系统竞争背后的利益竞逐 狼人:
    你必须知道的Windows Phone 7开发 狼人:
    展望Android之前世今生 狼人:
    在美做开发多年,写给国内iPhone开发新手 狼人:
    NDK入门项目实战 狼人:
  • 原文地址:https://www.cnblogs.com/FallDream/p/bzoj1934.html
Copyright © 2011-2022 走看看