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

    题解:

    不同集合付出代价

    有朋友关系的两点之间连边

    s向同意的连边,不同意的向t连边

    如果本来相对的朋友(x,y)都改变

    那么他们中间的边一定要被割掉,还是产生1的代价

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    using namespace std;
    const int maxn=1009;
    const int oo=1000000000;
    
    int n,m;
    
    struct Edge{
        int from,to,cap,flow;
    };
    vector<int>G[maxn];
    vector<Edge>edges;
    void Addedge(int x,int y,int z){
        Edge e;
        e.from=x;e.to=y;e.cap=z;e.flow=0;
        edges.push_back(e);
        e.from=y;e.to=x;e.cap=0;e.flow=0;
        edges.push_back(e);
        int c=edges.size();
        G[x].push_back(c-2);
        G[y].push_back(c-1);
    }
    
    int s,t;
    int vis[maxn];
    int d[maxn];
    queue<int>q;
    int Bfs(){
        memset(vis,0,sizeof(vis));
        vis[s]=1;d[s]=0;q.push(s);
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();++i){
                Edge e=edges[G[x][i]];
                if((!vis[e.to])&&(e.cap>e.flow)){
                    d[e.to]=d[x]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    
    int cur[maxn];
    int Dfs(int x,int a){
        if((x==t)||(a==0))return a;
        
        int nowflow=0,f=0;
        for(int i=cur[x];i<G[x].size();++i){
            cur[x]=i;
            Edge e=edges[G[x][i]];
            if((d[x]+1==d[e.to])&&((f=Dfs(e.to,min(a,e.cap-e.flow)))>0)){
                nowflow+=f;
                a-=f;
                edges[G[x][i]].flow+=f;
                edges[G[x][i]^1].flow-=f;
                if(a==0)break;
            }
        }
        return nowflow;
    }
    
    int Maxflow(){
        int flow=0;
        while(Bfs()){
            memset(cur,0,sizeof(cur));
            flow+=Dfs(s,oo);
        }
        return flow;
    }
    
    int main(){
        scanf("%d%d",&n,&m);
        s=n+1;t=s+1;
        for(int i=1;i<=n;++i){
            int x;scanf("%d",&x);
            if(x==0)Addedge(s,i,1);
            else Addedge(i,t,1);
        }
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            Addedge(x,y,1);
            Addedge(y,x,1);
        }
        
        cout<<Maxflow()<<endl;
        return 0;
    }
    自己还是太辣鸡了
  • 相关阅读:
    [题解] [HNOI2014] 世界树
    [luogu 5301][bzoj 5503] [GXOI/GZOI2019] 宝牌一大堆
    [HDU4507]吉哥系列故事——恨7不成妻
    [国家集训队]聪聪可可
    [模板]点分治
    [2018.8.12]模拟赛B组
    JZOJ5804. 【2018.08.12提高A组模拟】简单的序列
    2018.8.10模拟赛
    2018.8.8模拟赛
    [2018.8.6]模拟赛
  • 原文地址:https://www.cnblogs.com/zzyer/p/8609905.html
Copyright © 2011-2022 走看看