POJ 1149 PIGS 最大流建模
来源:互联网 发布:大数据与银行风险管理 编辑:程序博客网 时间:2024/06/05 08:06
点击打开链接
PIGS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14204 Accepted: 6305
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.
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.
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
题意:有M个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共最多能卖出多少头猪。(1 <= N <= 100, 1 <= M <= 1000)。
建图:
每个顾客分别用一个结点来表示。
对于每个猪圈的第一个顾客,从源点向他连一条边,容量就是该猪圈里的猪的初始数量。如果从源点到一名顾客有多条边,则可以把它们合并成一条,容量相加。
对于每个猪圈,假设有n个顾客打开过它,则对所有整数i∈[1, n),从该猪圈的第i个顾客向第i + 1个顾客连一条边,容量为∞。
从各个顾客到汇点各有一条边,容量是各个顾客能买的数量上限。
#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define INF 9999999int n,m;int map[300][300],pre[300],flow[300][300],p[300],a[300];int EK(int s,int t){ int sum=0; queue<int>q; memset(flow,0,sizeof(flow)); for(;;) { memset(a,0,sizeof(a));//记录残量 a[s]=INF; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=1; i<=t; i++) if(!a[i]&&map[u][i]>flow[u][i]) { p[i]=u;//记录i的父亲节的是u q.push(i); a[i]=a[u]<map[u][i]-flow[u][i]?a[u]:map[u][i]-flow[u][i]; } } if(!a[t])break;//如果残量是0的话,就找到最大流 for(int i=t; i!=s; i=p[i])//每条路加上最小残量 { flow[p[i]][i]+=a[t]; flow[i][p[i]]-=a[t]; } sum+=a[t];//记录流量 } return sum;}int main(){ int a,b,c,max; int pig[1007],link[1007]; while(scanf("%d%d",&n,&m)!=EOF) { memset(map,0,sizeof(map)); memset(p,0,sizeof(p)); memset(link,0,sizeof(link)); memset(pig,0,sizeof(pig)); int t=m+1; for(int i=1;i<=n;i++) scanf("%d",&pig[i]); for(int i=1;i<=m;i++) { scanf("%d",&a); for(int j=1;j<=a;j++) { scanf("%d",&b); if(link[b]==0) { link[b]=i; map[0][i]+=pig[b]; } else { map[link[b]][i]=INF; } } scanf("%d",&map[i][t]); } max=EK(0,t); printf("%d\n",max); } return 0;}
- POJ 1149 PIGS 最大流建模
- POJ 1149 PIGS(建模 + 最大流)
- POJ 1149 PIGS 【网络流建模汇总】最大流
- poj 1149 PIGS 【网络流经典建模】【求最大流】
- POJ 1149--PIGS 【最大流 && 处理重边 && 经典建模】
- 【POJ 1149】 Pigs 最大流
- [最大流] poj 1149 pigs
- POJ 1149 最大流 PIGS
- POJ 1149 PIGS (最大流)
- POJ 1149 PIGS 最大流
- poj 1149 PIGS 最大流
- POJ 1149 PIGS(最大流)
- POJ 1149 PIGS ,最大流
- 【最大流】POJ-1149 PIGS
- poj 1149 PIGS 最大流
- PIGS (poj 1149 最大流)
- POJ 1149 PIGS (最大流)
- POJ 1149 PIGS 最大流
- 海量数据处理分析
- Android 开机启动后执行
- isset 和empty 区别: php中is_null,empty,isset,unset 的区别详细介绍
- LINUX下查看设备信息的一些命令
- JQuery获取input type="text"中的值的各种方式
- POJ 1149 PIGS 最大流建模
- nfs
- poj-1579-Function Run Fun
- java 大数取余
- 关于WM_NCHITTEST消息
- Ubuntu下为Firefox安装Adobe Flash Player
- iOS多线程GCD
- 60 Commands of Linux : A Guide from Newbies to System Administrator
- 判断一个点是否在多边形内部