BNUOJ 1268 PIGS

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.

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

Croatia OI 2002 Final Exam - First day

解题:最大流问题。买猪。建图是关键啊!0为源n+1为汇。把买者看成流网络上的点,最先选择某个猪舍跟源点建立边,容量为这个猪舍猪头数,第二个选择某个猪舍的与第一个选择这个猪舍的买者建立边,容量为无穷大。类推。。。最后把最后与某个猪舍建立边的买者,再与汇建立边,数目为此买者的购买量!为什么这样建图呢?首先是源点!源点到第一个买者,我们考虑的不仅仅是第一个买者的购买力,还要考虑其他人的购买力,所以干脆把整个猪舍的猪都卖掉!假想可以卖完!至于中间的边,就是让尽可能的猪留给后面的人可以来买,但是最后的人的购买力有限,最多只能购买那么多!所以与汇邻接的边的权值为买者的购买量!最后流到汇的就是卖出的猪的头数。

之所以可以这样建图,是因为题意是指,第一个人打开某个猪舍后,这个猪舍就不会关了,这里面的猪可以随意卖给其他人。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <queue>
10 #define LL long long
11 #define INF 0x3f3f3f3f
12 using namespace std;
13 const int maxn = 1005;
14 int n,m,a[maxn],p[maxn];
15 int arc[maxn][maxn],pre[maxn],pigs[maxn];
16 int bfs(){
17     int u,v,f = 0;
18     queue<int>q;
19     while(true){
20         while(!q.empty()) q.pop();
21         memset(a,0,sizeof(a));
22         memset(p,0,sizeof(p));
23         a[0] = INF;
24         q.push(0);
25         while(!q.empty()){
26             u = q.front();
27             q.pop();
28             for(v = 0; v <= n+1; v++){
29                 if(!a[v] && arc[u][v] > 0){
30                     a[v] = min(a[u],arc[u][v]);
31                     p[v] = u;
32                     q.push(v);
33                 }
34             }
35             if(a[n+1]) break;
36         }
37         if(!a[n+1]) break;
38         for(u = n+1; u; u = p[u]){
39             arc[p[u]][u] -= a[n+1];
40             arc[u][p[u]] += a[n+1];
41         }
42         f += a[n+1];
43     }
44     return f;
45 }
46 int main(){
47     int i,j,k,buy,e;
48     while(~scanf("%d%d",&m,&n)){
49         memset(arc,0,sizeof(arc));
50         memset(pre,0,sizeof(pre));
51         for(i = 1; i <= m; i++)
52             scanf("%d",pigs+i);
53         for(i = 1; i <= n; i++){
54             scanf("%d",&k);
55             while(k--){
56                 scanf("%d",&e);
57                 if(pre[e]) arc[pre[e]][i] = INF;
58                 else arc[pre[e]][i] += pigs[e];
59                 pre[e] = i;
60             }
61             scanf("%d",&buy);
62             arc[i][n+1] += buy;
63         }
64         cout<<bfs()<<endl;
65     }
66     return 0;
67 }

BNUOJ 1268 PIGS,布布扣,bubuko.com

时间: 2024-12-17 19:32:02

BNUOJ 1268 PIGS的相关文章

POJ1149 PIGS [最大流 建图]

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 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 t

PIGS(最大流)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18742   Accepted: 8511 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 t

bnuoj 17184 代数

bnuoj 17184 代数 题意: 现有N个未知数A[1],A[2],-A[N],以及M个方程,每个方程都是形如A[s]+A[s+1]+A[s+2]+-A[t-1]+A[t]=c.现在求解这个方程组. 限制: 1 <= n,m <= 1e5; 1 <= s,t <= N; 0 <= c < 1e9 思路: 带权并查集. 这道题想了好久没想通,最后才知道还可以用并查集做,涨知识了. 感谢http://blog.csdn.net/balloons2012/article/

POJ 1149 PIGS(最大流)

POJ 1149 PIGS 题目链接 题意:有n个猪圈,m个顾客,猪圈中一开始有一些猪,顾客轮流来(注意是有先后顺序的),然后每个顾客会开启一些猪圈,在开启的猪圈中最多买b只猪,之后可以任意把剩下的猪分配到开着的猪圈中,问最多能卖出几只猪 思路:这题的关键在于建模,由于顾客有先后顺序,假如后来的顾客会开启x门,前面一个顾客也会开启x门,那么前面顾客相当与可以分配给后面顾客, 所以建模的方式为,源点和每个猪圈连,容量为猪圈猪数,每个猪圈和第一个开的顾客连,如果后面有顾客会开这个猪圈,则和之前的顾客

URAL 1268. Little Chu 求最大原根

题目来源:URAL 1268. Little Chu 题意:输入n 求一个最大的k 使得k^1 k^2 k^3...k^x mod n 后各不相同 思路:mod n 后各不相同 最多有 n个 那么此事k就是原根 因为k <= n 所以从n开始向下枚举 求一个最大的原根 #include <cstdio> #include <cstring> using namespace std; typedef long long LL; int p[100000], c; LL pow_

BNUOJ 1206 A Plug for UNIX

A Plug for UNIX Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 108764-bit integer IO format: %lld      Java class name: Main You are in charge of setting up the press room for the inaugural meeting of the Un

BNUOJ 17286 Dollars

Dollars Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 14764-bit integer IO format: %lld      Java class name: Main New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 1

POJ 1149 PIGS 最大流

第一次做网络流,看着教材里面的题解做的= = 用的是Ford,应该是最好理解的把,就是不断的找有没有从源点到汇点的增广路然后更新. 建图真是难啊,而且感觉细节要注意的地方比较多,一开始没有考虑反向弧,WA了两发,sad... #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <strin

BNUOJ 34025 -Poor Warehouse Keeper(贪心)

题目:BNUOJ 34025 -Poor Warehouse Keeper(贪心) 题目大意:有一个商品的信息表,上面是数量,下面是总价,然后旁边各有一个按钮.上面的数量按钮按一下数量就加1,然后价格对应的也要在加上一个当前的单价.下面的按钮按一下的话,就对应的总价加1.初始状态是 1 1,然后给出终点状态,问能否得到.可以的话输出最少要按几次按钮,否则输出-1:总价每次输出都是下取整. 解题思路:如果想要尽快的得到总点状态的值,那么就应该按总价的按钮,因为只有总价变大了,商品对因的单价就上升了