POJ1149(最大流)
PIGS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21678 | Accepted: 9911 |
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
Sample Input
3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6
Sample Output
7
Source

//2017-08-23
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[N<<]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].w = ;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} struct Dinic{
int level[N], S, T;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
} }dinic; int house[M]; int main()
{
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
int n, m;
while(cin>>m>>n){
int s = , t = n+;
dinic.init(s, t);
for(int i = ; i <= m; i++)
cin>>house[i];
int k, v;
int book[M];
memset(book, , sizeof(book));
for(int i = ; i <= n; i++){
cin>>k;
int weight = ;
while(k--){
cin>>v;
if(!book[v]){
book[v] = i;
weight += house[v];
}else{
add_edge(book[v], i, INF);
}
}
if(weight)add_edge(s, i, weight);
cin>>v;
add_edge(i, t, v);
}
cout<<dinic.maxflow()<<endl;
}
return ; }
POJ1149(最大流)的更多相关文章
- poj1149最大流经典构图神题
题意:n个顾客依次来买猪,有n个猪房,每个顾客每次可以开若干个房子,买完时,店主可以调整这位顾客 开的猪房里的猪,共m个猪房,每个猪房有若干猪,求最多能卖多少猪. 构图思想:顾客有先后,每个人想要的猪 ...
- POJ1149 最大流经典建图PIG
题意: 有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...
- poj 1273.PIG (最大流)
网络流 关键是建图,思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化). ...
- POJ-1149 PIGS---最大流+建图
题目链接: https://vjudge.net/problem/POJ-1149 题目大意: M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买这些有钥匙的猪圈里的猪,而且要买一定数量的猪,每 ...
- POJ1149 PIGS 【最大流 + 构图】
题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ1149 PIGS(最大流)
题意: 有一个人,他有m个猪圈,每个猪圈里面有一定数量的猪,但是每个猪圈的门都是锁着的,他自己没有钥匙,只有顾客有钥匙,一天依次来了n个顾客,(记住是依次来的)他们每个人都有一些钥匙,和他 ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- 【POJ1149&BZOJ1280】PIGS(最大流)
题意:Emmy在一个养猪场工作.这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙. 顾客会到养猪场来买猪,一个接着一个.每一位顾客都会有一些猪圈的钥匙,他们会将这些猪圈打开并买走固定数目的猪. 所有顾 ...
- poj1149 PIGS 最大流(神奇的建图)
一开始不看题解,建图出错了.后来发现是题目理解错了. if Mirko wants, he can redistribute the remaining pigs across the unlock ...
随机推荐
- pycharm光标变成黑框,恢复成竖线
pycharm光标变成黑框,如下图所示 解决办法: 按外接键盘上的Insert键,即可恢复.
- urllib2 的使用与介绍
爬虫简介 什么是爬虫? 爬虫:就是抓取网页数据的程序. HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的 ...
- Linux巩固记录(8) Hbase shell 基本使用
继续前几篇内容,讲解hbase基本使用 1.进入hbase shell: hbase有很多种操作方式,比如shell,java客户端,webUI等,可以直接输入hbase进行提示 [root@mast ...
- spring mvc开发过程中的乱码问题
在保证jsp,xml,数据库,编辑器编码一致的情况下. 1,用户输入中文,后台接收到也是中文,但是保存到数据库时乱码, 解决方法: 链接数据库的url="jdbc:mysql://local ...
- SQLAlchemy的ORM
表关系: 表之间的关系存在三种:一对一.一对多.多对多.而SQLAlchemy中的ORM也可以模拟这三种关系.因为一对一其实在SQLAlchemy中底层是通过一对多的方式模拟的,所以先来看下一对多的关 ...
- 09-02 java 多态
多态的前提.多态的特点 /* 多态:同一个对象(事物),在不同时刻体现出来的不同状态. 举例: 猫是猫,猫是动物. 水(液体,固体,气态). 多态的前提: A:要有继承关系. B:要有方法重写. 其实 ...
- 什么是NAT端口映射?
背景:我们访问百度的时候输入www.baidu.com出现的网站首页.发生了什么事情?百度它有服务器,IP地址是公网的,有自己的域名,所以你可以正常访问到百度(实际上访问的是IP地址+服务端口).如果 ...
- 使用.NET Core与Google Optimization Tools实现员工排班计划Scheduling
上一篇说完<Google Optimization Tools介绍>,让大家初步了解了Google Optimization Tools是一款约束求解(CP)的高效套件.那么我们用.NET ...
- discuz 数据库文件密码修改
网站系统需要修改的位置有两处 Discuz 和 UC-center ①路径:/wwwroot/config/config_global.php 这个根据你网站安装的路径而定. 打开 config_gl ...
- php -- 日期时间
----- 017-datetime.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&qu ...