poj 2584 T-Shirt Gumbo 最大匹配

题目

题目链接:http://poj.org/problem?id=2584

题目来源:http://www.cnblogs.com/vongang/archive/2012/02/21/2361882.html

题解

人和T恤(相同型号的拆开)建二分图,可以穿的就连上边,跑最大匹配, |M|=n 则可以。

也可以直接人和型号建流量图,直接跑最大流, s 到所有人容量为1,人到型号合法的就连容量为 1 的边,然后型号到t有一条容量为型号数量限制的边。直接跑最大流。

数据量比较小,怎么搞都可以。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 20;
const int TSIZE = 5;
const int M = N * TSIZE;
const int E = N * M;

struct Gragh {
    struct Edge {
        int to, nxt;
        Edge(int to, int nxt) : to(to), nxt(nxt) {}
        Edge() {}
    };

    Edge e[E];
    int head[N], match[M], ec;
    bool vis[N];

    void addEdge(int from, int to) {
        e[ec] = Edge(to, head[from]);
        head[from] = ec++;
    }
    void init(int n, int m) {
        ec = 0;
        for (int i = 0; i <= n; i++) head[i] = -1;
        for (int i = 0; i <= m; i++) match[i] = -1;
    }
    bool dfs(int x) {
        vis[x] = true;
        for (int i = head[x]; ~i; i = e[i].nxt) {
            int to = e[i].to, w = match[to];
            if (w == -1 || (!vis[w] && dfs(w))) {
                match[to] = x;
                return true;
            }
        }
        return false;
    }
    int bipatiteMatch(int n) {
        int ans = 0;
        for (int i = 0; i < n; i++) {
            memset(vis, 0, sizeof vis);
            if (dfs(i)) ans++;
        }
        return ans;
    }
};

Gragh g;

char s[N];
char sz[N] = "SMLXT";
PII a[N];

int main() {
    int n, x;
    while (scanf("%s", s) && strcmp(s, "ENDOFINPUT") != 0) {
        scanf("%d", &n);
        g.init(n, M-1);
        for (int i = 0; i < n; i++) {
            scanf("%s", s);
            int l = strchr(sz, s[0]) - sz;
            int r = strchr(sz, s[1]) - sz;
            a[i] = make_pair(l, r);
        }
        for (int i = 0; i < TSIZE; i++) {
            scanf("%d", &x);
            for (int j = 0; j < n; j++) {
                if (i < a[j].fi || i > a[j].se) continue;
                for (int k = 0; k < x; k++) {
                    g.addEdge(j, N * i + k);
                }
            }
        }
        puts(g.bipatiteMatch(n) == n ? "T-shirts rock!" : "I'd rather not wear a shirt anyway...");
        scanf("%s", s);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值