poj 2584 T-Shirt Gumbo 最大匹配
2016-05-12 20:38
393 查看
题目
题目链接:http://poj.org/problem?id=2584题目来源:/article/5121507.html
题解
人和T恤(相同型号的拆开)建二分图,可以穿的就连上边,跑最大匹配,|M|=n|M|=n则可以。也可以直接人和型号建流量图,直接跑最大流,ss到所有人容量为11,人到型号合法的就连容量为11的边,然后型号到tt有一条容量为型号数量限制的边。直接跑最大流。
数据量比较小,怎么搞都可以。
代码
#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 , match[M], ec; bool vis ; 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 ; char sz = "SMLXT"; PII a ; 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; }
相关文章推荐
- OCR资源
- DP练习 最长上升子序列nlogn解法
- StringBuilder的使用
- 操作系统实验三进程调度
- excel使用技巧
- session失效刷新后登录页面嵌入在iframe中的解决办法
- angular.js之路由实现
- Execbeat
- Spark运行模式(一)-----Spark独立模式
- Spring Ioc-依赖注入的几种方式
- HDU1864巴什博奕(水题)
- 如何在eclipse上配置maven
- 动态规划—Problem Q
- java19子类实例化过程
- acm_小母牛问题
- 【BZOJ 2157】旅游
- 如何领取门票参加中国北京科技产业博览会?
- Python 编程第一讲:IDE推荐
- poj 1573
- 编程珠玑:位图法排序