BNUOJ 1268 PIGS
2024-09-30 18:22:42
PIGS
Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1149
64-bit integer IO format: %lld Java class name: Main
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 arives, 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 arives, 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 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6
Sample Output
7
Source
解题:最大流问题。买猪。建图是关键啊!0为源n+1为汇。把买者看成流网络上的点,最先选择某个猪舍跟源点建立边,容量为这个猪舍猪头数,第二个选择某个猪舍的与第一个选择这个猪舍的买者建立边,容量为无穷大。类推。。。最后把最后与某个猪舍建立边的买者,再与汇建立边,数目为此买者的购买量!为什么这样建图呢?首先是源点!源点到第一个买者,我们考虑的不仅仅是第一个买者的购买力,还要考虑其他人的购买力,所以干脆把整个猪舍的猪都卖掉!假想可以卖完!至于中间的边,就是让尽可能的猪留给后面的人可以来买,但是最后的人的购买力有限,最多只能购买那么多!所以与汇邻接的边的权值为买者的购买量!最后流到汇的就是卖出的猪的头数。
之所以可以这样建图,是因为题意是指,第一个人打开某个猪舍后,这个猪舍就不会关了,这里面的猪可以随意卖给其他人。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <queue>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int n,m,a[maxn],p[maxn];
int arc[maxn][maxn],pre[maxn],pigs[maxn];
int bfs(){
int u,v,f = ;
queue<int>q;
while(true){
while(!q.empty()) q.pop();
memset(a,,sizeof(a));
memset(p,,sizeof(p));
a[] = INF;
q.push();
while(!q.empty()){
u = q.front();
q.pop();
for(v = ; v <= n+; v++){
if(!a[v] && arc[u][v] > ){
a[v] = min(a[u],arc[u][v]);
p[v] = u;
q.push(v);
}
}
if(a[n+]) break;
}
if(!a[n+]) break;
for(u = n+; u; u = p[u]){
arc[p[u]][u] -= a[n+];
arc[u][p[u]] += a[n+];
}
f += a[n+];
}
return f;
}
int main(){
int i,j,k,buy,e;
while(~scanf("%d%d",&m,&n)){
memset(arc,,sizeof(arc));
memset(pre,,sizeof(pre));
for(i = ; i <= m; i++)
scanf("%d",pigs+i);
for(i = ; i <= n; i++){
scanf("%d",&k);
while(k--){
scanf("%d",&e);
if(pre[e]) arc[pre[e]][i] = INF;
else arc[pre[e]][i] += pigs[e];
pre[e] = i;
}
scanf("%d",&buy);
arc[i][n+] += buy;
}
cout<<bfs()<<endl;
}
return ;
}
最新文章
- DX系列之TreeList
- KVM 虚拟机联网方式:NAT 和 Bridge
- LeetCode OJ-- Sort List **@
- Tarjian算法求强联通分量
- sp_executesql的执行计划会被重用(转载)
- String 去重,区分大小写
- 原生态Ajax实例
- html5桌面通知,notification的使用,右下角出现通知框
- 使用<;pre>;标签为你的网页加入大段代码
- join 数据库
- YT工作日志-0911
- OVS VxLAN Flow 分析 - 每天5分钟玩转 OpenStack(149)
- JavaScript一个生成文档目录的实例
- Golang atomic
- 递归&;栈帧空间
- DockerFile指令集
- 数据库实例: STOREBOOK >; 用户 >; 编辑 用户: MGMT_VIEW
- Workflow_上传和下载Workflow编译方式(汇总)
- thinkphp curd的事务回滚 一看就会
- Visio2010新建E-R图
热门文章
- linux的SHELL编程
- 区间DP UVA 10739 String to Palindrome
- 01背包 Codeforces Round #267 (Div. 2) C. George and Job
- Spring配置文件中未引入dubbo命名空间头部配置而引起的错误案例
- Java Annontation(注解)详解
- Oracle的一些名词和概念
- 冒泡排序算法和简单选择排序算法的js实现
- Pycharm+Django+Python+MySQL开发 后台管理数据库
- Bmob使用心得
- Apache与IIS端口冲突解决方法