题目链接:http://poj.org/problem?id=1149

PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:24094   Accepted: 10982

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.

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.
题目大意:
1.给定m个猪棚,n个顾客,接下来n行每行代表第i个顾客有k个猪棚的钥匙,以及该顾客的需求量。
2.注意题目的要求,主人可以在顾客打开了猪棚的时候对猪棚中的猪数量进行调动,所以,为了尽可能满足所有顾客的需求,建图需要体现对猪的调动。
3.对于每一个猪棚,第一个打开它的顾客,我们将源点向该顾客建边,容量为猪棚的猪数量。对于以后再次光顾该猪棚的顾客,我们将第一次光顾该猪棚的顾客与以后光顾该猪棚的顾客建边,容量为inf。最后将每个顾客与终点连边,边的容量为顾客的需求量。至于为什么这样建图,可以理解为将猪全部给了第一个光顾该猪棚的顾客,以后再需要的顾客可以从他这里拿,这样的话可以确保尽可能多的满足后来的顾客。
代码如下:
 #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 【最大流 + 构图】的更多相关文章

  1. POJ 1149 - PIGS - [最大流构图]

    Time Limit: 1000MS Memory Limit: 10000K Description Mirko works on a pig farm that consists of M loc ...

  2. POJ1149 PIGS [最大流 建图]

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description ...

  3. poj1149 PIGS 最大流(神奇的建图)

    一开始不看题解,建图出错了.后来发现是题目理解错了.  if Mirko wants, he can redistribute the remaining pigs across the unlock ...

  4. POJ1149 PIGS(最大流)

    题意:       有一个人,他有m个猪圈,每个猪圈里面有一定数量的猪,但是每个猪圈的门都是锁着的,他自己没有钥匙,只有顾客有钥匙,一天依次来了n个顾客,(记住是依次来的)他们每个人都有一些钥匙,和他 ...

  5. 网络流 - 最大流构图入门 bzoj 1305

    一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩 ...

  6. 【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 ...

  7. POJ1149 PIGS

    想了好久啊...(#-.-) 开始想到m*n个点的构图,明显超时,于是考虑压缩节点个数 我们发现每个猪圈最后被有且只有一个人调整,于是想到对于一个人,连接他能调整的每个猪圈的上一个控制人.(不懂可以开 ...

  8. PIGS(最大流)

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18742   Accepted: 8511 Description ...

  9. POJ1149 PIGS 【最大流量】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16555   Accepted: 7416 Description ...

随机推荐

  1. .net SerialPort

    虚拟串口并定时向虚拟串口定时发数据 http://scorpiomiracle.iteye.com/blog/653923 C#中如何使用SerialPort控件向单片机发送数据? http://zh ...

  2. skb head/data/tail/end/介绍

    2017年04月26日 18:21:12 abcLinux 阅读数 799   This first diagram illustrates the layoutof the SKB data are ...

  3. centos6中安装VMware Tools

    使用的是centos6.8,其他6版本方法大致相同. 1 .工具/原料1)安装过虚拟机软件的计算机2)linux操作系统 3)虚拟机配置VMware tools文件, 点击工具栏上的[虚拟机],然后选 ...

  4. WIN10环境下点击通知栏图标时自动切换输入法导致图标位置变动

    这个问题由来已久,每当点击系统右下角任务栏中的按钮时,原本是搜狗输入法就会自动变成“US [ 中文(简体,中国) ]”,图标会自动错位,导致响应的是其他功能. 假设上图是正常的环境,此时我点击电池图标 ...

  5. python+Django+mysql环境搭建

    为什么我的毕业设计还要用到网站啊啊啊啊.什么鬼啊,又要做爱拍拍又要做网站???饶了我啊..我选择狗带.. 网站就用django做吧,毕竟之前做过一个电脑销售网站,希望能借鉴一下经验什么的,不要一切从头 ...

  6. java课后实验性问题5

    课后作业一:字符串加密 程序设计思想: 从键盘获取字符串,将字符串转为字符数组,将每个元素加事前协定的“key”,再转为字符串输出. 程序流程图: 源代码: import java.util.Scan ...

  7. 【Centos】搭建 SVN 服务器

    1.如果仅仅只是搭建 svn 服务器: (a).先检查 svn 是否已经安装了 rpm -qa subversion #输入这个命令后,会出现 subversion 版本号   (b).如果没有安装, ...

  8. php判断变量类型

    php判断变量类型 一.总结 一句话总结: gettype()函数:gettype(1);返回的是integer is_array():is系列函数 1.PHP empty.isset.isnull的 ...

  9. 如何卸载oracle11g

    方法/步骤   .关闭oracle所有的服务.可以在windows的服务管理器中关闭:   打开注册表:regedit 打开路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentCo ...

  10. Linux ubuntu centos 下 grep显示前后几行信息

    标准unix/linux下的grep通过下面参数控制上下文 grep -C 5 foo file 显示file文件里匹配foo字串那行以及上下5行grep -B 5 foo file 显示foo及前5 ...