POJ-1149 network flow

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24426 Accepted: 11127
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

Meaning of the questions: is a sale of pigs to pig farmers, there are M room, pigs locked in the room, but that she did not have a key, people buy pigs with N, everybody do have one A key, namely K1, K2, K3. . . KA room key, so people can buy these rooms inside the pig, pig needs to buy B pigs. Every time people buy to buy a pig after pig farmers can put some pigs rushed to the room to others inside the room, these rooms are the people who buy pigs that can be opened, and between each room you can hold an unlimited number of pigs.
At that time less watched two conditions, see the blog's time to see that Italy can understand, one can catch the pig, the other is to buy a pig in the order, otherwise it will buy pigs to buy No. 2, then 3 room 2 pig arrived the room, and then everyone can buy a pig, inconsistent with sample answers. So this is in order to buy pigs.
Q. A total of how much you can sell pigs

Sample Explanation: 3 rooms, respectively, 3,110 pigs.
3 to buy a pig man, the first two keys, able to open 12 rooms, two pigs need
later without explanation
can then be pig farmers sold a total of seven pigs
first bought the first one pig, pig bought a room and pig farmers put the rest of the room 1 1 2 pigs rushed to the room, then 2 rooms have two pigs
second buy pigs to buy 3 rooms 3 pigs
third buy 2 rooms 2 pigs
in total 7

Ideas: network flow, from the source to each buy a pig who built side, traffic is the number of pigs needed to buy a pig in each room to carve the construction side, the flow rate is the number of pigs of the original room, the room was built to Meeting Point side, traffic is the original number of rooms pigs.
The first does not explain, and the second is the number of the original rooms of pigs, means: because the conditions of hog, pig every room might go to the other room, and later to buy a pig will buy these pigs, but the the remaining number of pigs flow to other rooms seem less likely, ** so we can have the same key (that is, able to open the same room) who built one side, traffic is INF, ** so that we can convert it, since you can put a pig rushed to a room, it can be arrived at our room the same, then how much I want to buy a pig, you can buy your way through, so that the edge is the first to point to the later, traffic it is INF, because there can then later I came to buy a pig ~~ by your way through my way (prohibition matryoshka) ~ ~ do not explain the third, then the network flow sleeve board on it.
AC Code:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int maxm=5e5+10;
const int maxn=3000;
const int INF=0x7f7f7f7f;
struct Edge{
	int next,v,val;
}map[maxm];
int head[maxn],sum;
int n,m,S,E,maxflow;
int num[maxn],dep[maxn],gap[maxn];

void Init()
{
	memset(map,0,sizeof(map));
	memset(head,-1,sizeof(head));
	sum=0;
	return ;
}
void addedge(int u,int v,int w)
{
	map[sum].v=v;
	map[sum].val=w;
	map[sum].next=head[u];
	head[u]=sum++;
	map[sum].v=u;
	map[sum].val=0;
	map[sum].next=head[v];
	head[v]=sum++;
	return ;
}
void bfs()
{
	memset(dep,-1,sizeof(dep));
	memset(gap,0,sizeof(gap));
	dep[E]=0;gap[0]=1;
	queue<int> q;
	q.push(E);
	while (!q.empty())
		{
			int u=q.front();
			q.pop();
			for (int i=head[u];i!=-1;i=map[i].next)
				{
					int v=map[i].v;
					if (dep[v]!=-1) continue;
					q.push(v);
					dep[v]=dep[u]+1;
					gap[dep[v]]++;
				}
		}
	//cout<<dep[S]<<"---------"<<endl;
	return ;
}
int dfs(int u,int flow)
{
	if (u==E)
		{
			maxflow+=flow;
			//cout<<maxflow<<" --== ==-- "<<endl;	
			//cout<<dep[S]<<endl;
			return flow;
		}
	int used=0;
	for (int i=head[u];i!=-1;i=map[i].next)
		{
			int v=map[i].v;
			if (map[i].val>0&&dep[v]+1==dep[u])
				{
					//cout<<u<<" -> "<<v<<" "<<map[i].val<<endl;
					int meta=dfs(v,min(map[i].val,flow-used));
					if (meta>0)
						{
							map[i].val-=meta;
							map[i^1].val+=meta;
							used+=meta;
						}
					if (used==flow) return used; 
				}
		}
	--gap[dep[u]];
	if (gap[dep[u]]==0) dep[S]=E+1;
	dep[u]++;
	gap[dep[u]]++;
	return used;
}
int ISAP()
{
	maxflow=0;
	bfs();
	while (dep[S]<E) dfs(S,INF);
	return maxflow;
}

int main(){
	vector<int> bri[maxn];
	int fl[maxn];
	int w,k;
	cin>>m>>n;
	S=0;E=n+m+1;
	Init();
	for (int i=1;i<=m;i++){
		scanf("%d",&fl[i]);	//记录每个房间原来猪的数量
		addedge(n+i,E,fl[i]);	//房间和汇点建边
	}
	for (int i=1;i<=n;i++){
		scanf("%d",&k);
		for (int j=1;j<=k;j++) {
			scanf("%d",&num[j]);	
			bri[num[j]].push_back(i);	//记录可以打开房间的买猪人
		}
		scanf("%d",&w);
		for (int j=1;j<=k;j++) 
			addedge(i,n+num[j],fl[num[j]]);//买猪人和房间建边
		addedge(S,i,w);		//源点和买猪人建边
	}
	for (int i=1;i<=m;i++){
		k=bri[i].size();
		for (int j=k-1;j>0;j--)
			addedge(bri[i][j],bri[i][j-1],INF);	//从后往前人人之间建边
	}
	int ans=ISAP();
	cout<<ans<<endl;
	return 0;
}
Published 46 original articles · won praise 2 · Views 3195

Guess you like

Origin blog.csdn.net/z1164754004z/article/details/104597588