题目链接: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. P4295 [SCOI2003]严格N元树 DP

    思路:DP 提交:\(5\)次 错因:2次高精写错(我太菜了),2次写错特判 题解: 设\(f[i]\)表示深度\(\leq i\)的严格\(n\)元树的数目,有 \[f[i]=pow(f[i-1], ...

  2. 51nod 1594 Gcd and Phi 反演

    OTZ 又被吊打了...我当初学的都去哪了??? 思路:反演套路? 提交:\(1\)次 题解: 求\(\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(gcd(\varphi(i ...

  3. 007_linuxC++之_构造函数的初级应用

    (一)构造函数:用来在创建对象时初始化对象, 即为对象成员变量赋初始值 (二)构造函数的命名必须和类名完全相同 (三)更对具体的查看:构造函数 (四)直接分析程序 运行结果 解析上面程序: 1. 当程 ...

  4. Activiti服务类- FormService服务类

    转自:https://www.cnblogs.com/liuqing576598117/p/9814953.html 1.获取//通过流程定义ID获取表单字段集合StartFormData start ...

  5. java上传文件夹

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用.此控件PC全平台支持包括mac,linux系统的文件上传,文章末尾将附上控件下载与教程链接 ...

  6. [Luogu] 产生数

    题面:https://www.luogu.org/problemnew/show/P1037 题解:https://www.zybuluo.com/wsndy-xx/note/1145473

  7. [51Nod] 配对

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 求出树的重心,跑spfa #include <iostre ...

  8. ZOJ 3182 HDU 2842递推

    ZOJ 3182 Nine Interlinks 题目大意:把一些带标号的环套到棍子上,标号为1的可以所以操作,标号i的根子在棍子上时,只有它标号比它小的换都不在棍子上,才能把标号为i+1的环,放在棍 ...

  9. java枚举类型总结

    java中的枚举类型是jdk1.5新增的一个东西,其本质是一个java.lang.Enum类的子类,每个枚举项是一个静态常量对象,由编译器为每个枚举项分配ordinal和name,其中ordinal是 ...

  10. 无缓存I/O操作和标准I/O文件操作区别

    本文转载于:http://www.360doc.com/content/11/0521/11/5455634_118306098.shtml 首先,先稍微了解系统调用的概念:       系统调用,英 ...