POJ 1149 PIGS(最大流)

时间:2023-03-09 18:36:34
POJ 1149 PIGS(最大流)

Description

Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs.  All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.  More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses.  An unlimited number of pigs can be placed in every pig-house.  Write a program that will find the maximum number of pigs that he can sell on that day.

Input

The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.  The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.  The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line):  A K1 K2 ... KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, ..., KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

Output

The first and only line of the output should contain the number of sold pigs.

题目大意:有n个人m个猪圈,每个猪圈里有一些猪,这n个人要买猪,每一个人会打开数个猪圈,买一些猪,然后这些猪圈的猪可以移动到另一些猪圈,问最多能卖多少猪。

思路:最大流。最朴素的想法是每个人建m个猪圈,不过这样搞点太多了。可以看到,若当两个猪圈被同时打开过,那么这此时两个猪圈就可以看成一个猪圈了。于是,对每一个猪圈,从源点到第一个打开它的人连一条边,容量为猪数,然后这个人再往下一个打开这个猪圈的人连一条边,容量为无穷大(其实这样两个人之间可能会有多条无穷大的边,不过我懒得搞,随便啦AC就好)。然后再从每个人连一条边到汇点,容量为这个人要买的猪的数量。最大流为答案。

PS:之前读入n、m的顺序搞错了WA了一次。n和m一样的样例太可恶了>_<

代码(16MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = MAXN * MAXN;
const int INF = 0x7fffffff; struct SAP {
int head[MAXN], gap[MAXN], pre[MAXN], dis[MAXN], cur[MAXN];
int next[MAXE], to[MAXE], flow[MAXE];
int ecnt, n, st, ed; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; flow[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} void bfs() {
queue<int> que; que.push(ed);
memset(dis, 0x3f, sizeof(dis));
dis[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
++gap[dis[u]];
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p ^ ] && dis[v] > n) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int Max_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
int ans = , minFlow = INF, u;
for(int i = ; i <= n; ++i) {
cur[i] = head[i];
gap[i] = ;
}
bfs();
u = pre[st] = st;
while(dis[st] < n) {
bool flag = false;
for(int &p = cur[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[u] == dis[v] + ) {
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed) {
ans += minFlow;
while(u != st) {
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow;
}
minFlow = INF;
}
break;
}
}
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[v] < minDis) {
minDis = dis[v];
cur[u] = p;
}
}
if(--gap[dis[u]] == ) break;
++gap[dis[u] = minDis + ];
u = pre[u];
}
return ans;
}
} G; int n, m;
int last[];
int a[], b[]; int main() {
scanf("%d%d", &m, &n);
G.init();
int ss = n + , tt = n + ;
for(int i = ; i <= m; ++i) scanf("%d", &a[i]);
for(int i = ; i <= n; ++i) {
int x, b;
scanf("%d", &b);
for(int j = ; j <= b; ++j) {
scanf("%d", &x);
if(last[x] == ) G.add_edge(ss, i, a[x]);
else G.add_edge(last[x], i, INF);
last[x] = i;
}
scanf("%d", &x); G.add_edge(i, tt, x);
}
printf("%d\n", G.Max_flow(ss, tt, tt));
}