码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)

时间:2015-05-08 00:03:32      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:poj   网络最大流   dinic   


PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17598   Accepted: 7977

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.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

Source

Croatia OI 2002 Final Exam - First day

题目链接:http://poj.org/problem?id=1149

题目大意:m个猪圈,n个顾客,每个顾客有a把钥匙,k1,k2...ka,每个顾客想买b头猪,当每个顾客到来时,他将那些他拥有钥匙的猪圈全部打开,迈克从这些猪圈中挑出一些猪卖给他们,如果迈克愿意,迈克可以重新分配这些被打开的猪圈中的猪,当顾客离开时,猪圈将再次被锁上,注意:猪圈可容纳的猪的数量没有限制

题目分析:最大流的题,就是靠开脑洞建图:
1)单独设一个源点一个汇点
2)源点和每个猪圈的第一个顾客连边,边权是开始时猪圈中猪的数目
3)若源点和某个点之间有重边,则将权合并
4)顾客j紧跟在顾客i之后打开某个猪圈,则边<i,j>权为无穷大
5)每个顾客和汇点之间连边,边权为顾客希望买的猪的数目
建完图,闭眼Dinic,0ms

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int const INF = 0x3fffffff;
int const MAXN = 105;
int const MAXM = 1005;
int cap[MAXN][MAXN], d[MAXN];
int pnum[MAXM], last[MAXM];
int m, n;
int src, sink;

bool BFS()
{
	memset(d, -1, sizeof(d));
	queue <int> q;
	d[src] = 0;
	q.push(src);
	while(!q.empty())
	{
		int cur = q.front();
		q.pop();
		for(int i = 0; i <= sink; i++)
		{
			if(cap[cur][i] && d[i] == -1)
			{
				d[i] = d[cur] + 1;
				q.push(i);
			}
		}
	}
	return d[sink] != -1;
}

int DFS(int t, int flow)
{
	if(t == sink || flow == 0)
		return flow;
	int tmp = flow;
	for(int i = 0; i <= sink; i++)
	{
		if(d[i] == d[t] + 1 && cap[t][i])
		{
			int mi = DFS(i, min(flow, cap[t][i]));
			cap[t][i] -= mi;
			cap[i][t] += mi;
			flow -= mi;
		}
	}
	return tmp - flow;
}

void Dinic()
{
	int ans = 0;
	while(BFS())
		ans += DFS(src, INF);
	printf("%d\n", ans);
}

int main()
{
	memset(pnum, 0, sizeof(pnum));
	memset(last, 0, sizeof(last));
	scanf("%d %d", &m, &n);
	src = 0;
	sink = n + 1;
	for(int i = 1; i <= m; i++)
		scanf("%d", &pnum[i]);
	for(int i = 1; i <= n; i++)
	{
		int num;
		scanf("%d", &num);
		for(int j = 0; j < num; j++)
		{
			int id;
			scanf("%d", &id);
			if(last[id] == 0)
				cap[src][i] += pnum[id];
			else
				cap[last[id]][i] = INF;
			last[id] = i;
		}
		scanf("%d", &cap[i][sink]);
	}
	Dinic();
}


POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)

标签:poj   网络最大流   dinic   

原文地址:http://blog.csdn.net/tc_to_top/article/details/45568577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!