poj1149
来源:互联网 发布:淘宝店铺头像图片免费 编辑:程序博客网 时间:2024/06/05 19:15
PIGS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17919 Accepted: 8149
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
题意: 有 M 个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依次来了 N 个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。分析: 此题是网络流,网络流里经典的构图题。将顾客看作除源和汇以外的节点,源和每个猪圈的第一个顾客连边,边的权是开始时猪圈中猪的数目,若源和某个节点之间有重边,则将权合并,每个猪圈的前后相邻两顾客之间连边,由前一个顾客指向后一个顾客。每个顾客和汇之间连边,边的权是顾客所希望购买的猪的数目。网络流,需要对图进行化简化简规则:规律 1. 如果几个节点的流量的来源完全相同,且流量为+∞,则可以把它们合并成一个。规律 2. 如果几个节点的流量的去向完全相同,且流量为+∞,则可以把它们合并成一个。规律 3. 如果从点 u 到点 v 有一条容量为 +∞ 的边,并且 u 是 v 的唯一流量来源,或者 v 是 u 的唯一流量去向,则可以把 u 和 v 合并成一个节点。代码:
type edge=record y,r,next,op:longint; end;var g:array[0..10000] of edge; level,q,h,pig,c,peo:array[0..10010] of longint; cus:array [1..1010,1..1010] of longint; n,m,f,i,j,a,b,tot,vs,ii,vt,x,y,k:longint; ans:qword; s:string;
function bfs:boolean;var i,f,r,tmp,v,u:longint;begin fillchar(level,sizeof(level),0); f:=1; r:=1; q[f]:=vs; level[vs]:=1; repeat v:=q[f]; tmp:=h[v]; while tmp<>-1 do begin u:=g[tmp].y; if (g[tmp].r<>0) and (level[u]=0) then begin level[u]:=level[v]+1; inc(r); q[r]:=u; if u=vt then exit(true); end; tmp:=g[tmp].next; end; inc(f); until f>r; exit(false);end;
function min(x,y:longint):longint;begin if x<y then exit(x); exit(y);end;
function dfs(v,a:longint):longint;var ans,flow,tmp,u,value:longint;begin if (v=vt) or (a=0) then exit(a); ans:=0; tmp:=h[v]; while tmp<>-1 do begin u:=g[tmp].y; value:=g[tmp].r; if (level[u]=level[v]+1) then begin flow:=dfs(u,min(a,value)); if flow<>0 then begin g[tmp].r:=g[tmp].r-flow; g[g[tmp].op].r:=g[g[tmp].op].r+flow; ans:=ans+flow; a:=a-flow; if a=0 then break; end; end; tmp:=g[tmp].next; end; exit(ans);end;
procedure add(a,b,c:longint);begin inc(tot); g[tot].y:=b; g[tot].r:=c; g[tot].next:=h[a]; h[a]:=tot; g[tot].op:=tot+1; inc(tot); g[tot].y:=a; g[tot].r:=0; g[tot].next:=h[b]; h[b]:=tot; g[tot].op:=tot-1;end;begin fillchar(h,sizeof(h),$ff); readln(n,m); tot:=0; ans:=0; vs:=0; vt:=m+1; for i:=1 to n do read(pig[i]); readln; for i:=1 to m do begin read(x); for j:=1 to x do begin read(y); inc(c[y]); cus[y,c[y]]:=i; end; read(y); readln; peo[i]:=y; end; for i:=1 to n do begin add(vs,cus[i,1],pig[i]); for j:=2 to c[i] do add(cus[i,j-1],cus[i,j],maxlongint); end; for i:=1 to m do add(i,vt,peo[i]); while bfs do begin ans:=ans+dfs(vs,maxlongint); end; writeln(ans);end.
0 0
- poj1149
- poj1149
- POJ1149
- poj1149
- POJ1149
- poj1149
- poj1149
- POJ1149
- poj1149 PIGS
- poj1149(pigs)
- POJ1149:PIGS
- poj1149 - PIGS
- poj1149 PIGS
- poj1149 PIGS
- poj1149 PIGS
- POJ1149 PIGS
- POJ1149 PIGS
- poj1149 pigs
- 学习MFC的消息通信问题
- 几种常见的排序方法(C语言实现)
- [LeetCode][Java] Spiral Matrix
- CoreAnimation编程指南(五)图层内容
- 如何在改版的spring官网下载jar包问题
- poj1149
- perl的变量、数组、哈希1
- 收集的java一些资料
- Unity3D 游戏引擎之控制模型移动旋转与碰撞
- hdu 1016 Prime Ring Problem
- 一、通过发送系统广播让android关机、重启
- CoreAnimation编程指南(六)动画
- C#笔记 垃圾回收garbage collection
- ROS学习手记12 -- ROS问题检测工具roswtf