POJ 1149:PIGS 网络流经典题

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18345 Accepted: 8354

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

 Mirko工作在一个有M间猪房的农场里,但是他没有钥匙不能打开这些房间。有趣的是,顾客有要买哪些猪的房间的钥匙。购买的时候Mirko就可以打开相应的房间,然后也可以调整相应房间里猪的数量关系。问在一定顾客与购买关系的条件下,Mirko最多能卖多少猪。

 这个构图求最大网络流的想法太赞,我真是远远想不到。

 构造图,构造一个源点S和汇点t。

 顾客是除了源点和汇点以外的节点,并且把每个猪房的第一个顾客与源点相连,权值为该猪房的起始猪的数量,假设第一个购买第N个猪房与第一个购买第M个猪房的人是一个人,则将其合并成一个点。

 顾客I在顾客J之后购买相同猪房中的猪,则有一条边从顾客J指向顾客I,权值为无穷大,因为是要从那个边过来的网络流中取一部分。(这种构图方法谁想的,太厉害了。)

 每一个顾客都与汇点连一条边,边的权值是每一个顾客希望购买的猪的数量。

 这种构图方式最让我瞠目结舌的在于它居然就真的全照顾到了题目所要求的所有条件,一开始我觉得这些条件真多真繁琐啊,构图怎么可能直接求一个网络流就完事了呢,以为图论的问题都是像之前那么直接套模板了,结果这种构图方式,哇~

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

const int sum = 201;
const int INF = 99999999;  

int cap[sum][sum],flow[sum][sum],a[sum],p[sum];
int val[1005];
int flag[1005];
int N,M;

void Edmonds_Karp()  
{
	int u,t,result=0;
	queue <int> s;
	while(s.size())s.pop();

	while(1)
	{
		memset(a,0,sizeof(a));
		memset(p,0,sizeof(p));

		a[0]=INF;
		s.push(0);

		while(s.size())
		{
			u=s.front();
			s.pop();

			for(t=0;t<=M+1;t++)
			{
				if(!a[t]&&flow[u][t]<cap[u][t])
				{
					s.push(t);
					p[t]=u;
					a[t]=min(a[u],cap[u][t]-flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量
				}
			}
		}
		if(a[M+1]==0)
			break;
		result += a[M+1];

		for(u=M+1;u!=0;u=p[u])
		{
			flow[p[u]][u] += a[M+1];
			flow[u][p[u]] -= a[M+1]; 
		}
	}
	cout<<result<<endl;
}

int main()
{
	int i,j,num,out,u,v,value;
	while(scanf("%d%d",&N,&M)==2)
	{
		memset(cap,0,sizeof(cap));
		memset(flow,0,sizeof(flow));
		memset(flag,0,sizeof(flag));

		for(i=1;i<=N;i++)
		{
			scanf("%d",&val[i]);
		}
		
		for(i=1;i<=M;i++)
		{
			scanf("%d",&num);
			for(j=1;j<=num;j++)
			{
				scanf("%d",&value);
				if(!flag[value])
				{
					flag[value]=i;
					cap[0][i] += val[value];
				}
				else
				{
					cap[flag[value]][i] = INF;
					flag[value]=i;
				}
			}
			scanf("%d",&out);
			cap[i][M+1] += out;
		}
		Edmonds_Karp();  
	}
	return 0;
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
森林防火应急联动指挥系统是一个集成了北斗定位/GPS、GIS、RS遥感、无线网络通讯、4G网络等技术的现代化智能系统,旨在提高森林火灾的预防和扑救效率。该系统通过实时监控、地图服务、历史数据管理、调度语音等功能,实现了现场指挥调度、语音呼叫通讯、远程监控、现场直播、救火人员生命检测等工作的网络化、智能化、可视化。它能够在火灾发生后迅速组网,确保现场与指挥中心的通信畅通,同时,系统支持快速部署,适应各种极端环境,保障信息的实时传输和历史数据的安全存储。 系统的设计遵循先进性、实用性、标准性、开放性、安全性、可靠性和扩展性原则,确保了技术的领先地位和未来的发展空间。系统架构包括应急终端、无线专网、应用联动应用和服务组件,以及安全审计模块,以确保用户合法性和数据安全性。部署方案灵活,能够根据现场需求快速搭建应急指挥平台,支持高并发视频直播和大容量数据存储。 智能终端设备具备三防等级,能够在恶劣环境下稳定工作,支持北斗+GPS双模定位,提供精确的位置信息。设备搭载的操作系统和处理器能够处理复杂的任务,如高清视频拍摄和数据传输。此外,设备还配备了多种传感器和接口,以适应不同的使用场景。 自适应无线网络是系统的关键组成部分,它基于认知无线电技术,能够根据环境变化动态调整通讯参数,优化通讯效果。网络支持点对点和点对多点的组网模式,具有低功耗、长距离覆盖、强抗干扰能力等特点,易于部署和维护。 系统的售后服务保障包括安装实施服务、系统维护服务、系统完善服务、培训服务等,确保用户能够高效使用系统。提供7*24小时的实时故障响应,以及定期的系统优化和维护,确保系统的稳定运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值