poj 1274(二分图最大匹配)
2014-02-20 23:07
381 查看
题意:题目就是求二分图最大匹配。
思路:直接使用匈牙利算法,主要还是用来熟悉一下模版。
代码如下:
View Code
思路:直接使用匈牙利算法,主要还是用来熟悉一下模版。
代码如下:
/************************************************** * Author : xiaohao Z * Blog : http://www.cnblogs.com/shu-xiaohao/ * Last modified : 2014-02-20 22:44 * Filename : poj_1274.cpp * Description : * ************************************************/ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #define MP(a, b) make_pair(a, b) #define PB(a) push_back(a) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<unsigned int,unsigned int> puu; typedef pair<int, double> pid; typedef pair<ll, int> pli; typedef pair<int, ll> pil; const int INF = 0x3f3f3f3f; const double eps = 1E-6; const int LEN = 1010; vector<int> Map[LEN]; int xn, yn, n, vis[LEN], match[LEN]; bool dfs(int v){ vis[v] = 1; for(int i=0; i<Map[v].size(); i++){ int u = Map[v][i], w = match[u]; if(w < 0 || (!vis[w] && dfs(w))){ match[v] = u; match[u] = v; return true; } } return false; } int hungary(){ int ret = 0; memset(match, -1, sizeof match); for(int i=0; i<n; i++){ if(match[i] < 0){ memset(vis, 0, sizeof vis); if(dfs(i)) ret ++; } } return ret; } int main() { // freopen("in.txt", "r", stdin); while(scanf("%d%d", &xn, &yn)!=EOF){ for(int i=0; i<LEN; i++) Map[i].clear(); for(int i=0; i<xn; i++){ int tn, b; scanf("%d", &tn); for(int j=0; j<tn; j++){ scanf("%d", &b);b--; Map[i].PB(b+xn); Map[b+xn].PB(i); } } n = xn + yn; int ans = hungary(); printf("%d\n", ans); } return 0; }
View Code
相关文章推荐
- POJ-1274 The Perfect Stall(二分图最大匹配)
- POJ 题目1274 The Perfect Stall(二分图最大匹配)
- POJ 1274-The Perfect Stall(二分图_最大匹配)
- 二分图最大匹配题目汇总 POJ 1274、2239、3020、3715
- 【二分图+最大匹配】北大 poj 1274 The Perfect Stall
- POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
- poj 1274(网络流解二分图的最大匹配)
- Poj 1274 The Perfect Stall[二分图最大匹配]
- POJ 1274 The Perfect Stall (二分图最大匹配入门题,匈牙利算法)
- POJ 1274 / POJ 1469 / POJ 2239 二分图最大匹配
- POJ 1274 The Perfect Stall(二分图最大匹配)
- 二分图最大基数匹配 POJ 1274 The Perfect Stall
- POJ 1274 / POJ 1469 / POJ 2239 二分图最大匹配
- POJ-1274-The Perfect Stall【二分图最大匹配】【模板题】
- POJ 1274--The Perfect Stall【二分图 && 最大匹配数 && 水题】
- POJ 1274 二分图最大匹配简单单向
- POJ1274 The Perfect Stall [二分图最大匹配 匈牙利算法]
- POJ 1274 The Perfect Stall 二分图最大匹配
- POJ1274 The Perfect Stall【二分图最大匹配】
- POJ-1274 The Perfect Stall (二分图最大匹配模板题)