zoukankan      html  css  js  c++  java
  • BZOJ 3442 学习小组

    题解:

    神建图

    普通的二分图费用流建完后

    添加学生x->t 容量为k-1的边

     表示尽量让x参加一个活动,剩下的k-1次机会可以不参加

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    const int maxn=300;
    const int oo=1000000000;
    
    int n,m,k;
    int a[maxn],b[maxn];
    int ans=0;
    
    struct Edge{
        int from,to,cap,flow,cost;
    };
    vector<int>G[maxn];
    vector<Edge>edges;
    void Addedge(int x,int y,int z,int w){
        Edge e;
        e.from=x;e.to=y;e.cap=z;e.flow=0;e.cost=w;
        edges.push_back(e);
        e.from=y;e.to=x;e.cap=0;e.flow=0;e.cost=-w;
        edges.push_back(e);
        int c=edges.size();
        G[x].push_back(c-2);
        G[y].push_back(c-1);
    }
    
    int s,t,totn;
    queue<int>q;
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    
    int Spfa(int &nowflow,int &nowcost){
        for(int i=1;i<=totn;++i){
            d[i]=oo;inq[i]=p[i]=0;
        }
        d[s]=0;q.push(s);
        while(!q.empty()){
            int x=q.front();q.pop();inq[x]=0;
            for(int i=0;i<G[x].size();++i){
                Edge e=edges[G[x][i]];
                if((e.cap>e.flow)&&(d[x]+e.cost<d[e.to])){
                    d[e.to]=d[x]+e.cost;
                    p[e.to]=G[x][i];
                    if(!inq[e.to]){
                        q.push(e.to);
                        inq[e.to]=1;
                    }
                }
            }
        }
        
        if(d[t]==oo)return 0;
        int x=t,f=oo;
        while(x!=s){
            Edge e=edges[p[x]];
            f=min(f,e.cap-e.flow);
            x=e.from;
        }
        nowflow+=f;nowcost+=f*d[t];
        x=t;
        while(x!=s){
            edges[p[x]].flow+=f;
            edges[p[x]^1].flow-=f;
            x=edges[p[x]].from;
        }
        return 1;
    }
    
    int Mincost(){
        int flow=0,cost=0;
        while(Spfa(flow,cost)){
        }
        return cost;
    }
    
    char ss[200];
    int main(){
        scanf("%d%d%d",&n,&m,&k);
        totn=n+m;s=++totn;t=++totn;
        for(int i=1;i<=m;++i)scanf("%d",&a[i]);
        for(int i=1;i<=m;++i)scanf("%d",&b[i]);
        for(int i=1;i<=m;++i){
            for(int j=1;j<=n;++j){
                Addedge(i+n,t,1,(2*j-1)*a[i]-b[i]);
            }
        }
        for(int i=1;i<=n;++i){
            scanf("%s",ss+1);
            for(int j=1;j<=m;++j){
                if(ss[j]=='1')Addedge(i,j+n,1,0);
            }
        }
        for(int i=1;i<=n;++i)Addedge(s,i,k,0);
        for(int i=1;i<=n;++i)Addedge(i,t,k-1,0);
        cout<<Mincost()<<endl;
        return 0;
    }
    自己还是太辣鸡了
  • 相关阅读:
    POJ2778 DNA Sequence AC自动机上dp
    codeforces732F Tourist Reform 边双联通分量
    codeforces786B Legacy 线段树优化建图
    洛谷P3588 PUS 线段树优化建图
    codeforces1301D Time to Run 模拟
    codeforces1303B National Project 二分或直接计算
    codeforces1303C Perfect Keyboard 模拟或判断欧拉路
    codeforces1303D Fill The Bag 二进制应用+贪心
    python之路——使用python操作mysql数据库
    python之路——mysql索引原理
  • 原文地址:https://www.cnblogs.com/zzyer/p/8609779.html
Copyright © 2011-2022 走看看