• UVA10249 The Grand Dinner(最大流)


    题意:

    N个队伍在M张桌子上吃饭,问你,是否存在一种方案,使得队伍中每个人都在不同的桌子上吃饭,若不能输出0,若能,输出1并输出解决方案

    分析:最大流问题,只是这个需要输出解决方案.先建图,每个队伍Ai与每个桌子Bi建一条边,权值为1,因为每个队伍只能派一个人在这张桌子上吃饭.同时建立超级源点S和超级汇点T,S向每个队伍建一条边,权值为队伍人数.每张桌子向T建一条边,权值为桌子容量!

    求最大流ans,若ans等于所有人数,则有解决方案,否则无解决方案;

    对与解决方案,对与每条边,起始点要求是队伍标号,终点是桌子标号,且流量flow要与容量cap相等,说明这条边被选择了!

    // File Name: 10249.cpp
    // Author: Zlbing
    // Created Time: 2013/4/22 16:14:33
    
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<cstdio>
    #include<set>
    #include<map>
    #include<vector>
    #include<cstring>
    #include<stack>
    #include<cmath>
    #include<queue>
    using namespace std;
    #define CL(x,v); memset(x,v,sizeof(x));
    #define INF 0x3f3f3f3f
    #define LL long long
    #define REP(i,r,n) for(int i=r;i<=n;i++)
    #define RREP(i,n,r) for(int i=n;i>=r;i--)
    
    const int MAXN=200;
    struct Edge{
        int from,to,cap,flow;
    };
    bool cmp(const Edge& a,const Edge& b){
        return a.from < b.from || (a.from == b.from && a.to < b.to);
    }
    struct Dinic{
        int n,m,s,t;
        vector<Edge> edges;
        vector<int> G[MAXN];
        bool vis[MAXN];
        int d[MAXN];
        int cur[MAXN];
        void init(int n){
            this->n=n;
            for(int i=0;i<=n;i++)G[i].clear();
            edges.clear();
        }
        void AddEdge(int from,int to,int cap){
            edges.push_back((Edge){from,to,cap,0});
            edges.push_back((Edge){to,from,0,0});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
            m=edges.size();
            G[from].push_back(m-2);
            G[to].push_back(m-1);
        }
        bool BFS(){
            CL(vis,0);
            queue<int> Q;
            Q.push(s);
            d[s]=0;
            vis[s]=1;
            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){
                        vis[e.to]=1;
                        d[e.to]=d[x]+1;
                        Q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
        int DFS(int x,int a){
            if(x==t||a==0)return a;
            int flow=0,f;
            for(int& i=cur[x];i<G[x].size();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){
                    e.flow+=f;
                    edges[G[x][i]^1].flow-=f;
                    flow+=f;
                    a-=f;
                    if(a==0)break;
                }
            }
            return flow;
        }
        //当所求流量大于need时就退出,降低时间
        int Maxflow(int s,int t,int need){
            this->s=s;this->t=t;
            int flow=0;
            while(BFS()){
                CL(cur,0);
                flow+=DFS(s,INF);
                if(flow>need)return flow;
            }
            return flow;
        }
        //最小割割边
        vector<int> Mincut(){
            BFS();
            vector<int> ans;
            for(int i=0;i<edges.size();i++){
                Edge& e=edges[i];
                if(vis[e.from]&&!vis[e.to]&&e.cap>0)ans.push_back(i);
            }
            return ans;
        }
        void Reduce(){
            for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
        }
        void ClearFlow(){
            for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
        }
    };
    Dinic solver;
    int A[MAXN],B[MAXN];
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            if(n==0&&m==0)break;
            solver.init(n+m+1);
            int sum=0;
            int s=0,t=n+m+1;
            REP(i,1,n){
                scanf("%d",&A[i]);
                sum+=A[i];
            }
            REP(i,1,m)scanf("%d",&B[i]);
            REP(i,1,n)
                REP(j,1,m)
                solver.AddEdge(i,n+j,1);
            REP(i,1,n)
                solver.AddEdge(s,i,A[i]);
            REP(i,1,m)
                solver.AddEdge(i+n,t,B[i]);
            int ans=solver.Maxflow(s,t,INF);
            if(ans==sum)
            {
                vector<int> C[MAXN];
                for(int i=0;i<solver.edges.size();i++)
                {
                    Edge e=solver.edges[i];
                    //printf("e.from--%d  e.to--%d\n",e.from,e.to);
                    if(e.from>=1&&e.from<=n&&e.to>n&&e.to<=n+m&&e.flow==e.cap){
                        C[e.from].push_back(e.to-n);
                    }
                }
                printf("1\n");
                for(int i=1;i<=n;i++){
                for(int j=0;j<C[i].size();j++)
                {
                    if(!j)printf("%d",C[i][j]);
                    else printf(" %d",C[i][j]);
                }
                    printf("\n");
                }
            }
            else printf("0\n");
    
        }
        return 0;
    }
  • 相关阅读:
    Vue.js
    Vue.js
    Vue.js
    Vue.js
    webpack(1)
    webpack(2)
    babel-loader7和babel8版本的问题
    [JZOJ4274] 终章-剑之魂
    [JZOJ427] 圣章-精灵使的魔法语
    BZOJ题表(红色表示已完成)
  • 原文地址:https://www.cnblogs.com/arbitrary/p/3035946.html
Copyright © 2020-2023  润新知