Test instructions
There are different numbers of pigs in each pigsty of M pigsty [0, 1000]
There are n customers each customer needs bi pig have AI key can open ai a different pigsty
A customer buys a pig in order and buys a pig with a key, and the pigs in the pigsty can transfer each other .
It's a fantastic problem, it's amazing to build a map
Build diagram:
The traffic for each customer and source point is the number of pigs connected to the customer's pigsty and the pig's pigsty before the pigsty is connected to the other customer and
Per customer to meeting point traffic is the number of pigs that the customer needs
Each customer needs to be connected to the same customer who selected the same pigsty because the pigsty can be swapped with each other without considering different pigsty.
/*****************************************************problem:1149user:g_lorymemory:632ktime:16mslanguage:c++ result:accepted*****************************************************/#include <iostream> #include < cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace Std;const int N = 2010;const int INF = 0x5f5f5f5f;int flow[n];int cap[n][n];int pre[n];queue<int> q;int bfs (int src, int. des) {Whil E (!q.empty ()) Q.pop (); memset (Pre,-1, sizeof pre); FLOW[SRC] = INF; PRE[SRC] =-1; Q.push (SRC); while (!q.empty ()) {int idx = Q.front (); Q.pop (); if (idx = = des) break; for (int i = 0; I <= des; ++i) {if (pre[i] = = 1 && cap[idx][i] > 0) { Pre[i] = idx; Flow[i] = min (Cap[idx][i], flow[idx]); Q.push (i); }}} if (pre[des] = =-1) return-1; return flow[des];} int Maxflow (intsrc, int des) {int ans = 0; int in = 0; while (in = BFS (src, des))! =-1) {int k = des; while (k! = src) {int last = Pre[k]; CAP[LAST][K]-= in; Cap[k][last] + = in; K = Last; } ans + = in; } return ans; int Pig[n];int Last[n];int Main () {int m, N; scanf ("%d%d", &m, &n); for (int i = 1; I <= m; ++i) scanf ("%d", &pig[i]); int num, a, B; for (int i = 1; I <= n; ++i) {scanf ("%d", &num); for (int j = 1; j <= Num; ++j) {scanf ("%d", &a); if (!last[a]) {Last[a] = i; Cap[0][i] + = Pig[a]; } else {cap[last[a]][i] = INF; Last[a] = i; }} scanf ("%d", &b); Cap[i][n + 1] = b; } printf ("%d\n", Maxflow (0, n + 1)); return 0;}
Poj1149--pigs (maximum flow)