POJ1149 PIGS 【最大流 + 构图】
题目链接:http://poj.org/problem?id=1149
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:24094 | Accepted: 10982 |
Description
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 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
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int MAXM = ; //猪圈数目上界
const int MAXN = ; //顾客数目上界
const int inf = 0x3f3f3f3f; int m, n; //猪棚数目 顾客数目
int num[MAXM], first[MAXM], vis[MAXM];//每个猪圈里猪的数目 每个猪圈第一个顾客 每个猪圈是否已经有第一个顾客买了
int head[MAXN], cnt;
int dep[MAXN];
queue<int> Q; struct Edge
{
int to, next, flow;
}edge[ * MAXN + * MAXN * MAXN]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].flow = c;
edge[cnt].next = head[a];
head[a] = cnt;
} int bfs(int st, int ed)
{
if(st == ed)
return ;
while(!Q.empty()) Q.pop();
mem(dep, -);
dep[st] = ;
Q.push(st);
while(!Q.empty())
{
int index = Q.front();
Q.pop();
for(int i = head[index]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(edge[i].flow > && dep[to] == -)
{
dep[to] = dep[index] + ;
Q.push(to);
}
}
}
return dep[ed] != -;
} int dfs(int now, int ed, int zx)
{
if(now == ed)
return zx;
for(int i = head[now]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dep[to] == dep[now] + && edge[i].flow > )
{
int flow = dfs(to, ed, min(zx, edge[i].flow));
if(flow > )
{
edge[i].flow -= flow;
edge[i ^ ].flow += flow;
return flow;
}
}
}
return -;
} void dinic(int st, int ed)
{
int ans = ;
while(bfs(st, ed))
{
while()
{
int inc = dfs(st, ed, inf);
if(inc == -)
break;
ans += inc;
}
}
printf("%d\n", ans);
} int main()
{
int m, n;
scanf("%d%d", &m, &n);
int st = , ed = n + ;
mem(head, -), cnt = -;
mem(first, -), mem(vis, );
for(int i = ; i <= m; i ++)
scanf("%d", &num[i]);
for(int i = ; i <= n; i ++)
{
int k;
scanf("%d", &k);
for(int j = ; j <= k; j ++)
{
int x;
scanf("%d", &x);//猪圈编号
if(!vis[x])
{
first[x] = i;
vis[x] = ;
add(st, i, num[x]);
add(i, st, );
}
else
{
add(first[x], i, inf);
add(i, first[x], );
}
}
int x;
scanf("%d", &x);
add(i, ed, x);
add(ed, i, );
}
dinic(st, ed);
return ;
}
POJ1149
POJ1149 PIGS 【最大流 + 构图】的更多相关文章
- POJ 1149 - PIGS - [最大流构图]
Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- poj1149 PIGS 最大流(神奇的建图)
一开始不看题解,建图出错了.后来发现是题目理解错了. if Mirko wants, he can redistribute the remaining pigs across the unlock ...
- POJ1149 PIGS(最大流)
题意: 有一个人,他有m个猪圈,每个猪圈里面有一定数量的猪,但是每个猪圈的门都是锁着的,他自己没有钥匙,只有顾客有钥匙,一天依次来了n个顾客,(记住是依次来的)他们每个人都有一些钥匙,和他 ...
- 网络流 - 最大流构图入门 bzoj 1305
一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...
- 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广
3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 174 Solved: 9 ...
- POJ1149 PIGS
想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...
- PIGS(最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18742 Accepted: 8511 Description ...
- POJ1149 PIGS 【最大流量】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16555 Accepted: 7416 Description ...
随机推荐
- mouseover([[data],fn])
mouseover([[data],fn]) 概述 当鼠标指针位于元素上方时,会发生 mouseover 事件. 该事件大多数时候会与 mouseout 事件一起使用.直线电机选型 注释:与 mous ...
- sql 存储过程记录
-- exec sp_helptext add_book1 CREATE proc add_book1 --创建存储过程 @DocCode VARCHAR() --创建参数 as BEGIN INSE ...
- 五十九.大数据、Hadoop 、 Hadoop安装与配置 、 HDFS
1.安装Hadoop 单机模式安装Hadoop 安装JAVA环境 设置环境变量,启动运行 1.1 环境准备 1)配置主机名为nn01,ip为192.168.1.21,配置yum源(系统源) 备 ...
- python+opencv 图像预处理
一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...
- centos7 配置静态ip时出现双ip问题解决
1.先用ifconfig,看看有几个网卡 2.执行vi /etc/sysconfig/network-scripts/ifcfg-eth0最后的网卡名字改为实际的 3.静态ip只需要设置着几个地方,B ...
- 2019腾讯前端技术大会资源TWeb
扫码关注公众号 回复“TWeb”即可获取“2019腾讯前端技术大会”的PPT
- CF1030C
CF1030C 题意: 给你一个数字,问能否拆分成k段,使得每一段的每一位数字相加结果相等. 解法: 考虑数位DP. 暴力按位考虑每一位是否满足条件 CODE: #include<cstdio& ...
- 2019balsn两道web和2019巅峰极客一道web记录
遇到3道有点意思的web,记录一下~ web1 题目地址:http://warmup.balsnctf.com/ 源码如下所示: <?php if (($secret = base64_deco ...
- WEB-INF目录下登录表单提交的重定向
问题描述 登陆表单提交跳转后刷新会重新提交表单,但是使用重定向时不走视图解析器,不能访问WEB-INF下的资源 解决方法 原方法 @RequestMapping(value = "/logi ...
- hey is a tiny program that sends some load to a web application.
hey is a tiny program that sends some load to a web application. DOS attack DOS攻击生成 https://github.c ...