Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Line 1: Three space-separated integers: N, F, and D
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fiintegers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
Output
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
Sample Input
4 3 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3
Sample Output
3
Hint
One way to satisfy three cows is:
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
牛吃草问题,这个牛是一个点,有因为每一头牛只能用一次,所以要进行拆点。
这个图很好建的,我就不说了。
#include <cstdio> #include <cstdlib> #include <queue> #include <vector> #include <iostream> #include <algorithm> #include <map> #include <cstring> #include <cmath> #include <string> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int maxn = 1000 + 10; struct edge { int u, v, c, f, cost; edge(int u, int v, int c, int f, int cost) :u(u), v(v), c(c), f(f), cost(cost) {} }; vector<edge>e; vector<int>G[maxn]; int a[maxn];//找增广路每个点的水流量 int p[maxn];//每次找增广路反向记录路径 int d[maxn];//SPFA算法的最短路 int inq[maxn];//SPFA算法是否在队列中 int s, t; void init() { for (int i = 0; i <= maxn; i++)G[i].clear(); e.clear(); } void add(int u, int v, int c, int cost) { e.push_back(edge(u, v, c, 0, cost)); e.push_back(edge(v, u, 0, 0, -cost)); int m = e.size(); G[u].push_back(m - 2); G[v].push_back(m - 1); } bool bellman(int s, int t, int& flow, long long & cost) { memset(d, inf, sizeof(d)); memset(inq, 0, sizeof(inq)); d[s] = 0; inq[s] = 1;//源点s的距离设为0,标记入队 p[s] = 0; a[s] = INF;//源点流量为INF(和之前的最大流算法是一样的) queue<int>q;//Bellman算法和增广路算法同步进行,沿着最短路拓展增广路,得出的解一定是最小费用最大流 q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0;//入队列标记删除 for (int i = 0; i < G[u].size(); i++) { edge & now = e[G[u][i]]; int v = now.v; if (now.c > now.f && d[v] > d[u] + now.cost) //now.c > now.f表示这条路还未流满(和最大流一样) //d[v] > d[u] + e.cost Bellman 算法中边的松弛 { d[v] = d[u] + now.cost;//Bellman 算法边的松弛 p[v] = G[u][i];//反向记录边的编号 a[v] = min(a[u], now.c - now.f);//到达v点的水量取决于边剩余的容量和u点的水量 if (!inq[v]) { q.push(v); inq[v] = 1; }//Bellman 算法入队 } } } if (d[t] == INF)return false;//找不到增广路 flow += a[t];//最大流的值,此函数引用flow这个值,最后可以直接求出flow cost += (long long)d[t] * (long long)a[t];//距离乘上到达汇点的流量就是费用 for (int u = t; u != s; u = e[p[u]].u)//逆向存边 { e[p[u]].f += a[t];//正向边加上流量 e[p[u] ^ 1].f -= a[t];//反向边减去流量 (和增广路算法一样) } return true; } int MincostMaxflow(int s, int t, long long & cost) { cost = 0; int flow = 0; while (bellman(s, t, flow, cost));//由于Bellman函数用的是引用,所以只要一直调用就可以求出flow和cost return flow;//返回最大流,cost引用可以直接返回最小费用 } struct node { int x, y; node(int x=0,int y=0):x(x),y(y){} }; node peo[110], house[110]; char mp[110][110]; int main() { int n, m; while(cin>>n>>m) { init(); int cas = 0, tot = 0; if (n == 0 && m == 0) break; for (int i = 1; i <= n; i++) { cin >> mp[i] + 1; for(int j=1;j<=m;j++) { if (mp[i][j] == ‘m‘) peo[++cas] = node(i, j); if (mp[i][j] == ‘H‘) house[++tot] = node(i, j); } } s = 0, t = cas + tot + 1; for (int i = 1; i <= cas; i++) add(s, i, 1, 0); for (int i = 1; i <= tot; i++) add(cas + i, t, 1, 0); for(int i=1;i<=cas;i++) { for(int j=1;j<=tot;j++) { int cost = abs(peo[i].x - house[j].x) + abs(peo[i].y - house[j].y); add(i, j + cas, 1, cost); } } ll cost = 0; int ans = MincostMaxflow(s, t, cost); printf("%lld\n", cost); } return 0; }
原文地址:https://www.cnblogs.com/EchoZQN/p/10798003.html