USACO Section 4.2: The Perfect Stall
2013-07-25 01:45
274 查看
这题关键就在将题转换成最大流模板题。首先有一个原始点,N个cow个点, M个barn点和一个终点,原始点到cow点和barn点到终点的流都为1,而cow对应的barn就是cow点到对应barn点的流,为1.这样题目就转换成了原始点到终点的最大流问题
/* ID: yingzho1 LANG: C++ TASK: stall4 */ #include <iostream> #include <fstream> #include <string> #include <map> #include <vector> #include <set> #include <algorithm> #include <stdio.h> #include <queue> #include <cstring> #include <cmath> #include <list> #include <cstdio> #include <cstdlib> #include <limits> #include <stack> using namespace std; ifstream fin("stall4.in"); ofstream fout("stall4.out"); const int MAX = 210; const int INF = 2000000000; int N, M; int g[2*MAX][2*MAX], f[2*MAX][2*MAX], pre[2*MAX], inc[2*MAX]; bool bfs(int s, int d) { queue<int> que; for (int i = 1; i <= N+M+2; i++) pre[i] = -1; que.push(s); inc[s] = INF; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 1; i <= N+M+2; i++) { if (pre[i] == -1 && f[u][i] < g[u][i]) { inc[i] = min(inc[u], g[u][i]-f[u][i]); pre[i] = u; if (i == d) return true; que.push(i); } } } return false; } int edmond_karp(int s, int d) { int maxflow = 0; while (bfs(s, d)) { maxflow += inc[d]; for (int i = d; i != s; i = pre[i]) { f[pre[i]][i] += inc[d]; f[i][pre[i]] -= inc[d]; } } return maxflow; } int main() { fin >> N >> M; int s, d; for (int i = 1; i <= N; i++) { g[1][1+i] = 1; fin >> s; for (int j = 0; j < s; j++) { fin >> d; g[1+i][1+N+d] = 1; } } for (int i = N+2; i <= N+M+1; i++) g[i][N+M+2] = 1; fout << edmond_karp(1, N+M+2) << endl; return 0; }
相关文章推荐
- USACO Section 4.2 The Perfect Stall - 网络流求最大二分图匹配..
- USACO Section4.2 The Perfect Stall &&POJ1274
- USACO-Section 4.2 The Perfect Stall (二分图最大匹配[匈牙利算法[DFS]||网络流[Ford-Fulkerson]])
- POJ1274 The Perfect Stall_洛谷P1894 [USACO4.2]完美的牛栏
- USACO 4.2 The Perfect Stall网络流解最大二分图匹配
- (洛谷P1894)USACO 4.2 The Perfect Stall 完美的牛栏
- USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
- usaco 4.2 The Perfect Stall 二分图最大匹配
- USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配)
- USACO The Perfect Stall 4.2(二分图最大匹配,匈牙利算法)
- USCAO4.2 The Perfect Stall ,二分图最大匹配
- usaco 4.2 The Perfect Stall(二分图最大匹配入门题)
- USACO 4.2 The Perfect Stall(匈牙利)
- Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
- (ssl1904)USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配)
- USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配)
- USACO Section 4.2 The Perfect Stall(二分图匹配)
- The Perfect Stall_usaco4.2_匹配
- POJ 1274 The Perfect Stall(二分匹配-hungary)
- C++&Pascal&Python——【USACO 4.2.2】——The Perfect Stall