• POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)


    题意

    现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能够拿到自己尺码范围内的衣服.

    思路

    明显的二分图多重最大匹配问题:每个点能匹配的边不再限制1个,而是多个。 做法:最大流。虽说也有对应的匈牙利算法,但是我还是图省事用最大流做了。 建图:源点连接每个型号的衣服,容量为能够匹配的个数(数量),汇点连接每个队员,容量也为能够匹配的个数(1),其他匹配边容量为1。

    代码

     
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define MID(x,y) ((x+y)/2)
    #define mem(a,b) memset(a,b,sizeof(a))
    using namespace std;
    const int MAXV = 35;
    const int MAXE = 205;
    const int oo = 0x3fffffff;
    
    template 
    struct Dinic{
        struct flow_node{
            int u, v;
            T flow;
            int opp;
            int next;
        }arc[2*MAXE];
        int vn, en, head[MAXV];
        int cur[MAXV];
        int q[MAXV];
        int path[2*MAXE], top;
        int dep[MAXV];
        void init(int n){
            vn = n;
            en = 0;
            mem(head, -1);
        }
        void insert_flow(int u, int v, T flow){
            arc[en].u = u;
            arc[en].v = v;
            arc[en].flow = flow;
            arc[en].next = head[u];
            head[u] = en ++;
    
            arc[en].u = v;
            arc[en].v = u;
            arc[en].flow = 0;
            arc[en].next = head[v];
            head[v] = en ++;
        }
        bool bfs(int s, int t){
            mem(dep, -1);
            int lq = 0, rq = 1;
            dep[s] = 0;
            q[lq] = s;
            while(lq < rq){
                int u = q[lq ++];
                if (u == t){
                    return true;
                }
                for (int i = head[u]; i != -1; i = arc[i].next){
                    int v = arc[i].v;
                    if (dep[v] == -1 && arc[i].flow > 0){
                        dep[v] = dep[u] + 1;
                        q[rq ++] = v;
                    }
                }
            }
            return false;
        }
        T solve(int s, int t){
            T maxflow = 0;
            while(bfs(s, t)){
                int i, j;
                for (i = 1; i <= vn; i ++)  cur[i] = head[i];
                for (i = s, top = 0;;){
                    if (i == t){
                        int mink;
                        T minflow = 0x7fffffff;						//要比容量的oo大
                        for (int k = 0; k < top; k ++)
                            if (minflow > arc[path[k]].flow){
                                minflow = arc[path[k]].flow;
                                mink = k;
                            }
                        for (int k = 0; k < top; k ++)
                            arc[path[k]].flow -= minflow, arc[path[k]^1].flow += minflow;
                        maxflow += minflow;
                        top = mink;
                        i = arc[path[top]].u;
                    }
                    for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
                        int v = arc[j].v;
                        if (arc[j].flow && dep[v] == dep[i] + 1)
                            break;
                    }
                    if (j != -1){
                        path[top ++] = j;
                        i = arc[j].v;
                    }
                    else{
                        if (top == 0)   break;
                        dep[i] = -1;
                        i = arc[path[-- top]].u;
                    }
                }
            }
            return maxflow;
        }
    };
    Dinic  dinic;
    
    int main(){
    	//freopen("test.in", "r", stdin);
    	//freopen("test.out", "w", stdout);
    	string s;
    	while(cin >> s){
            if (s == "ENDOFINPUT")
                break;
            int n;
            cin >> n;
            dinic.init(n+7);
            for (int i = 1; i <= n; i ++){
                string tmp;
                cin >> tmp;
                dinic.insert_flow(i+5, n+7, 1);
                for (int j = 0; j < (int)tmp.size(); j ++){
                    switch (tmp[j]){
                    case 'S':
                        dinic.insert_flow(1, i+5, 1);
                    case 'M':
                        dinic.insert_flow(2, i+5, 1);
                    case 'L':
                        dinic.insert_flow(3, i+5, 1);
                    case 'X':
                        dinic.insert_flow(4, i+5, 1);
                    case 'T':
                        dinic.insert_flow(5, i+5, 1);
                        break;
                    }
                }
            }
            for (int i = 1; i <= 5; i ++){
                int num;
                cin >> num;
                dinic.insert_flow(n+6, i, num);
            }
            if (dinic.solve(n+6, n+7) == n){
                puts("T-shirts rock!");
            }
            else{
                puts("I'd rather not wear a shirt anyway...");
            }
            cin >> s;
        }
    	return 0;
    }
    
  • 相关阅读:
    [1041] XX easy problem
    [1027] 火焰纹章
    省赛选拔赛解题报告
    二维DP hdu 1421 搬寝室问题
    最长斐波那契子序列选取(离散化 + 二分 + DP)
    两个有序链表合成一个有序链表
    CodeForce 608B Hamming Distance Sum
    CodeForce 607A&&608C Chain Reaction
    (DP)最大价值事件序列选取
    面向对象>>>抽象类>>>鸭子类型>>>封装
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114074.html
Copyright © 2020-2023  润新知