poj1149
非常好的网络流
每个顾客分别用一个结点来表示。
对于每个猪圈的第一个顾客,从源点向他连一条边,容量就是该猪圈里的猪的初始数量
对于每个猪圈,假设有n个顾客打开过它,则对所有整数i∈[1, n),从该猪圈的第i个顾客向第i + 1个顾客连一条边,容量为无穷。
从各个顾客到汇点各有一条边,容量是各个顾客能买的数量上限。
其实很好理解
const inf=;
type node=record
from,point,flow,next:longint;
end;
var p,s,w,h,numh,cur,pre:array[..] of longint;
v:array[..] of boolean;
a:array[..,..] of longint;
edge:array[..] of node;
ans,z,len,t,i,j,k,x,y,n,m:longint; procedure add(x,y,f:longint);
begin
inc(len);
edge[len].from:=x;
edge[len].point:=y;
edge[len].flow:=f;
edge[len].next:=p[x];
p[x]:=len;
end; procedure sap;
var q,u,i,j,flow,neck,tmp:longint;
begin
fillchar(numh,sizeof(numh),);
fillchar(h,sizeof(h),);
fillchar(pre,sizeof(pre),);
numh[]:=t+;
u:=;
while h[]<t+ do
begin
if u=t then
begin
neck:=;
flow:=inf;
i:=;
j:=cur[i];
while i<>t do
begin
if flow>edge[j].flow then
begin
flow:=edge[j].flow;
neck:=i;
end;
i:=edge[j].point;
j:=cur[i];
end;
i:=;
j:=cur[i];
while i<>t do
begin
dec(edge[j].flow,flow);
inc(edge[j xor ].flow,flow);
i:=edge[j].point;
j:=cur[i];
end;
ans:=ans+flow;
u:=neck;
end;
q:=-;
i:=p[u];
while i<>- do
begin
x:=edge[i].point;
if (edge[i].flow>) and (h[u]=h[x]+) then
begin
q:=i;
break;
end;
i:=edge[i].next;
end;
if q<>- then
begin
cur[u]:=q;
pre[x]:=u;
u:=x;
end
else begin
dec(numh[h[u]]);
if numh[h[u]]= then break;
tmp:=t+;
i:=p[u];
while i<>- do
begin
x:=edge[i].point;
if (edge[i].flow>) then tmp:=min(tmp,h[x]);
i:=edge[i].next;
end;
h[u]:=tmp+;
inc(numh[h[u]]);
if u<> then u:=pre[u];
end;
end;
end; begin
readln(m,n);
fillchar(p,sizeof(p),);
len:=-;
t:=n+;
for i:= to m do
read(w[i]);
for i:= to n do
begin
read(y);
z:=;
for j:= to y do
begin
read(x);
if v[x]=false then
begin
v[x]:=true;
z:=z+w[x]; //如果多条边连接源点和顾客,那么合并
end;
inc(s[x]);
a[x,s[x]]:=i;
end;
if z<> then
begin
add(,i,z);
add(i,,);
end;
readln(x);
add(i,t,x);
add(t,i,);
end;
for i:= to m do
for j:= to s[i]- do
for k:=j+ to s[i] do
begin
add(a[i,j],a[i,k],inf);
add(a[i,k],a[i,j],);
end;
sap;
writeln(ans);
end.
poj1149的更多相关文章
- POJ1149 PIGS
想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...
- POJ-1149 PIGS---最大流+建图
题目链接: https://vjudge.net/problem/POJ-1149 题目大意: M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买这些有钥匙的猪圈里的猪,而且要买一定数量的猪,每 ...
- [BZOJ1280][POJ1149]Emmy卖猪pigs
[BZOJ1280][POJ1149]Emmy卖猪pigs 试题描述 Emmy在一个养猪场工作.这个养猪场有 \(M\) 个锁着的猪圈,但Emmy并没有钥匙.顾客会到养猪场来买猪,一个接着一个.每一位 ...
- POJ1149 PIGS 【最大流 + 构图】
题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- POJ1149 PIGS (网络流)
PIGS Time Limit: 1000MS M ...
- POJ1149 PIGS 【最大流量】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16555 Accepted: 7416 Description ...
- poj1149构图题
引题解: 这道题目的大意是这样的:⦁ 有 M 个猪圈(M ≤ 1000),每个猪圈里初始时有若干头猪.⦁ 一开始所有猪圈都是关闭的.⦁ 依次来了 N 个顾客(N ≤ 100),每个顾客分别会打开指定 ...
- POJ1149(最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21678 Accepted: 9911 Description ...
随机推荐
- 如何在eclipse中配置Selenium
1, Install python 33.(Python 27也可以) 2, Setup Selenium If you did not install Easy_install module, yo ...
- 四个基数任意次数组合相加得到一个数N,求所有可能组合
#include <iostream> #include <vector> usingnamespace std; vector<int> vec; constin ...
- Python数据结构——二叉树的实现
1. 二叉树 二叉树(binary tree)中的每个节点都不能有多于两个的儿子. 1.1 二叉树列表实现 如上图的二叉树可用列表表示: tree=['A', #root ['B', #左子树 ['D ...
- 选中excel中的对象
2007在查找和选择中点击“选择对象”,然后再全选全个sheet(ctrl+a)就可以看到了. 2010 “选择对象”在 开始——查找和选择——选择对象
- JSP页面时间动态显示 (转载)
<script type="text/javascript"> function startTime(){ var today=new Dat ...
- Windows.Andy.Code4App.dll Win8.1/WP8.1通用类库@ver1.0.1
在上篇 Windows.Andy.Code4App.dll Win8.1/WP8.1通用类库@ver1.0.0 已经对Win8.1和WP8.1部分扩展通用类库做了说明,这篇继续对通用类库做扩展.写的 ...
- 【BZOJ 1022】 [SHOI2008]小约翰的游戏John
Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取 ...
- DataTable经典报错{列/行已属于其他表}
Delete()之后需要datatable.AccepteChanges()方法确认完全删除,因为Delete()只是将相应列的状态标志为删除, 还可以通过datatable.RejectChange ...
- 在openwrt上初体验PostgreSQL数据库
要求 请确保在你的路由器shell 中有以下这些命令 adduser, deluser, addgroup, delgroup, su . 还需要熟悉su,chown ,opkg,mkdir,服务操作 ...
- java8 新特性
[转载]:http://www.importnew.com/11908.html 本文由 ImportNew - 刘 家财 翻译自 javacodegeeks.欢迎加入翻译小组.转载请见文末要求. 编 ...