1
12
2016
0

【POJ1149】【最大流】PIGS

PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19068   Accepted: 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 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

Source

题意:

有M个猪圈,每个猪圈里有若干头猪,起初所有猪圈都是关闭的。有N个顾客依次来买猪,每个顾客有一个需求量,并且会打开指定的几个猪圈从其中买猪。每个顾客走后,他打开的猪圈中的猪可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。我们可以选择卖给每个顾客的猪的数量,问能卖出的猪的总数的最大值。


解法:

我最初的想法是将顾客作为左部点,猪圈作为右部点,这样可以通过样例但是错误的建图方法。忽略了“每个顾客走后,他打开的猪圈中的猪可以被任意地调换到其它开着的猪圈里”这句关键的话。


把顾客作为点。建立源点和汇点。

从源点出发向第一个能打开猪圈i的顾客连一条边,容量为猪圈i里猪的数量。

从每个顾客出发向汇点连一条边,容量为该顾客的需求量。

如果顾客i在顾客j之前来买猪,并且顾客i和j都能打开某一个猪圈,那么从i到j连容量为正无穷的边。

注意:add(u,v,w1)+add(u,v,w2)<=>add(u,v,w1+w2),但后者可降低时间复杂度,前者会T

我T掉的代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define inf 1000000000
#define for2(i,s,t) for(int i=(s);i<=(t);i++)
using namespace std;
const int M=1500,N=150;
int m,n,s,t,pig[M],door,want,last[M];
int head[N],tot=1,maxflow,dis[N],cur[N],h[N];
bool vis[M];
struct edge{
	int go,next,v;
}e[M*N];
inline void addedge(int x,int y,int v)
{
	e[++tot]=(edge){y,head[x],v};head[x]=tot;
	e[++tot]=(edge){x,head[y],0};head[y]=tot;
}
bool bfs()//构造分层图 
{
	queue<int> q;
	for2(i,s,t) dis[i]=-1;
	q.push(s);dis[s]=0;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].next){
			int y=e[i].go;
			if(e[i].v&&dis[y]==-1){//dis兼有判断和记录的作用 
				dis[y]=dis[x]+1;
				q.push(y); 
			}
		}
	}
	return dis[t]!=-1; 
}
int dfs(int x,int f)
{
	if(x==t) return f;
	int tmp,used=0;
	for(int i=cur[x];i;i=e[i].next){//当前弧优化—从当前弧开始 
		int y=e[i].go;
		if(e[i].v&&dis[y]==dis[x]+1){
			tmp=dfs(e[i].go,min(e[i].v,f-used));
			e[i].v-=tmp;
			e[i^1].v+=tmp;
			if(e[i].v) cur[x]=i;
			used+=tmp;
			if(used==f) return f;
		}
	}
	if(!used) dis[x]=-1;
	return used;
}
void dinic()
{
	maxflow=0;
	while(bfs()){
		for(int i=s;i<=t;i++)
			cur[i]=head[i];//当前弧优化—更新当前弧 
		maxflow+=dfs(s,inf); 
	}
}
int main()
{
	memset(last,0,sizeof(last));
	memset(vis,false,sizeof(vis));//还没有人打开任何猪圈 
	scanf("%d%d",&m,&n);
	s=0;t=m+n+1;
	for2(i,1,m) scanf("%d",&pig[i]);
	for2(i,1,n){
		int tt;
		scanf("%d",&tt);
		while(tt--){
			scanf("%d",&door);
			if(!last[door]) addedge(s,i,pig[door]),last[door]=i;//last应为first 
			else addedge(last[door],i,inf);
		}
		scanf("%d",&want);
		addedge(i,t,want);
	}
	dinic();
	printf("%d\n",maxflow);
	return 0;
}

阿当AC的代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#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 1000000000
using 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);
}
Category: 网络流 | Tags: | Read Count: 372

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com