您的位置:首页 > 其它

POJ 1149 PIGS(最大流)

2013-09-04 14:18 417 查看
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 = 110;
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, 0, sizeof(head));
ecnt = 2;
}

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] = 0; next[ecnt] = head[v]; head[v] = ecnt++;
}

void bfs() {
queue<int> que; que.push(ed);
memset(dis, 0x3f, sizeof(dis));
dis[ed] = 0;
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 ^ 1] && dis[v] > n) {
dis[v] = dis[u] + 1;
que.push(v);
}
}
}
}

int Max_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
int ans = 0, minFlow = INF, u;
for(int i = 0; i <= n; ++i) {
cur[i] = head[i];
gap[i] = 0;
}
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] + 1) {
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] ^ 1] += minFlow;
}
minFlow = INF;
}
break;
}
}
if(flag) continue;
int minDis = n - 1;
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]] == 0) break;
++gap[dis[u] = minDis + 1];
u = pre[u];
}
return ans;
}
} G;

int n, m;
int last[1010];
int a[1010], b[110];

int main() {
scanf("%d%d", &m, &n);
G.init();
int ss = n + 1, tt = n + 2;
for(int i = 1; i <= m; ++i) scanf("%d", &a[i]);
for(int i = 1; i <= n; ++i) {
int x, b;
scanf("%d", &b);
for(int j = 1; j <= b; ++j) {
scanf("%d", &x);
if(last[x] == 0) 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));
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: