题目链接: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. kernel namespace

    reference: https://lwn.net/Articles/531114/

  2. at/crontab

    at yum -y install at systemctl start atd 增加任务 增加任务的第三行是 是ctrl+D 表示的是退出 第四行是系统提示任务执行的时间 任务查询 atq 任务删除 ...

  3. The 10th Shandong Provincial Collegiate Programming Contest

    目录 Contest Info Solutions A. Calandar B. Flipping Game C. Wandering Robot D. Game on a Graph E. BaoB ...

  4. Friends (ZOJ - 3710)

    Problem Alice lives in the country where people like to make friends. The friendship is bidirectiona ...

  5. 指针——可能我学的还只是c++的皮毛

    C. 线性链表的插入与删除 单点时限: 2.0 sec 内存限制: 256 MB 实现线性链表的插入与删除操作 只需完成给定函数的定义. NODE* insertLinklist(NODE* head ...

  6. python异常链

    习惯使用java开发,在java开发里有异常链概念和重新抛出异常,在python是怎么实现的呢? 1.异常链 1.1.java实现 public static void test1() throws ...

  7. Ryu控制器编程开发——packet_in和packet_out简易交换机实现

    Ryu控制器二次开发,实现一个简单的只能够简单地广播数据包的交换机. from ryu.base import app_manager from ryu.controller import ofp_e ...

  8. CTF辅助脚本

    首先推荐这篇文章,网上有多次转载,这是我见过日期比较早的 CTF中那些脑洞大开的编码和加密 凯撒密码 flag='flag{abcdef}' c='' n=20 for i in flag: if ' ...

  9. 重读APUE(14)-主线程终止对子线程的影响

    在main中创建线程,我们称main线程为主线程,新建线程为子线程(其实没有什么主线程和子线程的父子概念,它们是平行的,为了好理解这样称呼),如果子线程内部执行相对比较耗时的操作,主线程执行的快,而且 ...

  10. form表单无刷新提交

    Ajax最大的特点就是可以不刷新页面而实现数据的通信及更改页面信息.那么用AJAX进行后台通信传递字符串还是可以的,遇到上传文件该怎么办呢?基于安全考虑,JS是不能直接进行文件操作的,只好用原始的fr ...