poj1149 PIGS -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【meiwen.anslib.com - 电脑资料】

    PIGSTime Limit:1000MSMemory Limit:10000KTotal Submissions:19068Accepted:8697

    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 33 1 102 1 2 22 1 3 31 2 6

    Sample Output

7

    Source

Croatia OI 2002 Final Exam - First day

    最大流,

poj1149 PIGS

电脑资料

poj1149 PIGS》(http://meiwen.anslib.com)。

    构图方式:

    ①把每个顾客看作除源点和汇点以外的节点。

    ②从源点向每个猪圈的第一个顾客连一条边,容量为该猪圈最初的猪的数量。

    ③每个猪圈的前后两个顾客之间连一条边,容量为正无穷。因为可以任意分配每个猪圈中的猪的数量。

    ④从每个顾客向汇点连一条边,容量为要购买的猪的数量。

    这道题的构图方法很巧妙。

#include<iostream>#include<cstdio>#include#include<cmath>#include<cstring>#include<cstdlib>#include<queue>#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define LL long long#define pa pair<int,int>#define MAXN 105#define MAXM 1005#define INF 1000000000using namespace std;int n,m,k,x,s,t,cnt=1,ans=0;int pre[MAXM],head[MAXN],cur[MAXN],dis[MAXN],c[MAXN],a[MAXM];bool vst[MAXM],f[MAXN];struct edge_type{	int next,to,v;}e[10005];inline int read(){	int x=0,f=1;char ch=getchar();	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}	return x*f;}inline void add_edge(int x,int y,int v){	e[++cnt]=(edge_type){head[x],y,v};head[x]=cnt;	e[++cnt]=(edge_type){head[y],x,0};head[y]=cnt;}inline bool bfs(){	queue<int>q;	memset(dis,-1,sizeof(dis));	dis[s]=0;q.push(s);	while (!q.empty())	{		int tmp=q.front();q.pop();		if (tmp==t) return true;		for(int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1)		{			dis[e[i].to]=dis[tmp]+1;			q.push(e[i].to);		}	}	return false;}inline int dfs(int x,int f){	int tmp,sum=0;	if (x==t) return f;	for(int &i=cur[x];i;i=e[i].next)	{		int y=e[i].to;		if (e[i].v&&dis[y]==dis[x]+1)		{			tmp=dfs(y,min(f-sum,e[i].v));			e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp;			if (sum==f) return sum;		}	}	if (!sum) dis[x]=-1;	return sum;}inline void dinic(){	while (bfs())	{		F(i,1,n+2) cur[i]=head[i];		ans+=dfs(s,INF);	}}int main(){	memset(head,0,sizeof(head));	memset(c,0,sizeof(c));	memset(vst,false,sizeof(vst));	m=read();n=read();s=n+1;t=n+2;	F(i,1,m) a[i]=read();	F(i,1,n)	{		memset(f,false,sizeof(f));		k=read();		while (k--)		{			x=read();			if (!vst[x]) {vst[x]=true;c[i]+=a[x];}			if (pre[x]&&!f[pre[x]]) add_edge(pre[x],i,INF),f[pre[x]]=true;			pre[x]=i;		} 		x=read();if (x) add_edge(i,t,x);	}	F(i,1,n) if (c[i]) add_edge(s,i,c[i]);	dinic();	printf("%d\n",ans);}</int></int,int></queue></cstdlib></cstring></cmath></cstdio></iostream>

最新文章