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

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.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

Source

题目大意

 Mirko养着一些猪 猪关在一些猪圈里面 猪圈是锁着的

他自己没有钥匙(汗)

 仅仅有要来买猪的顾客才有钥匙

 顾客依次来 每一个顾客会用他的钥匙打开一些猪圈 买

走一些猪 然后锁上

 在锁上之前 Mirko有机会又一次分配这几个已打开猪圈

的猪

 如今给出一開始每一个猪圈的猪数 每一个顾客全部的钥匙

和要买走的猪数 问Mirko最多能卖掉几头猪

题解:对于每一个猪圈的第一个购买的人,加入一条源点到这个人的边,权为这个猪圈的猪数,对于后来的且想要购买该猪圈的人。加入一条第一个购买该猪圈的人到该人的边。权为inf,然后加入每一个人到汇点一条边,权值为该人想要购买的猪的头数。至此,构图完毕。

#include <stdio.h>
#include <string.h>
#define inf 0x3fffffff
#define maxn 110
#define maxm 1002 int pig[maxm], m, n, sink;
int G[maxn][maxn], queue[maxn];
bool vis[maxn]; int Layer[maxn]; bool countLayer() {
memset(Layer, 0, sizeof(Layer));
int id = 0, front = 0, now, i;
Layer[0] = 1; queue[id++] = 0;
while(front < id) {
now = queue[front++];
for(i = 0; i <= sink; ++i)
if(G[now][i] && !Layer[i]) {
Layer[i] = Layer[now] + 1;
if(i == sink) return true;
else queue[id++] = i;
}
}
return false;
} int Dinic() {
int minCut, pos, maxFlow = 0;
int i, id = 0, u, v, now;
while(countLayer()) {
memset(vis, 0, sizeof(vis));
vis[0] = 1; queue[id++] = 0;
while(id) {
now = queue[id - 1];
if(now == sink) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
if(G[u][v] < minCut) {
minCut = G[u][v];
pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(queue[id - 1] != pos)
vis[queue[--id]] = 0;
} else {
for(i = 0; i <= sink; ++i) {
if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) {
vis[i] = 1; queue[id++] = i; break;
}
}
if(i > sink) --id;
}
}
}
return maxFlow;
} int main() {
//freopen("stdin.txt", "r", stdin);
int i, keys, num;
while(scanf("%d%d", &m, &n) == 2) {
sink = n + 1;
for(i = 1; i <= m; ++i)
scanf("%d", &pig[i]);
memset(G, 0, sizeof(G));
for(i = 1; i <= n; ++i) {
scanf("%d", &keys);
while(keys--) {
scanf("%d", &num);
if(pig[num] >= 0) {
G[0][i] += pig[num]; // 0 is source
pig[num] = -i; // 这里是标记第num个猪圈联通的第一个人
} else G[-pig[num]][i] = inf;
}
scanf("%d", &G[i][sink]);
}
printf("%d\n", Dinic());
}
return 0;
}

2015.4.20

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxn = 105;
const int inf = 0x3f3f3f3f;
int G[maxn][maxn], M, N, S, T;
int pigHouse[maxn*10]; int Dinic(int s, int t); void getMap()
{
memset(G, 0, sizeof(G));
S = 0; T = N + 1; int i, j, K, pos;
for (i = 1; i <= M; ++i)
scanf("%d", &pigHouse[i]); for (i = 1; i <= N; ++i) {
scanf("%d", &K);
while (K--) {
scanf("%d", &pos);
if (pigHouse[pos] >= 0) {
G[S][i] += pigHouse[pos];
pigHouse[pos] = -i;
} else {
G[-pigHouse[pos]][i] = inf;
}
}
scanf("%d", &G[i][T]);
}
} void solve()
{
cout << Dinic(S, T) << endl;
} int main()
{
while (cin >> M >> N) {
getMap();
solve();
}
return 0;
} int queue[maxn];
bool vis[maxn]; int Layer[maxn];
bool countLayer(int s, int t) {
memset(Layer, 0, sizeof(Layer));
int id = 0, front = 0, now, i;
Layer[s] = 1; queue[id++] = s;
while(front < id) {
now = queue[front++];
for(i = s; i <= t; ++i)
if(G[now][i] && !Layer[i]) {
Layer[i] = Layer[now] + 1;
if(i == t) return true;
else queue[id++] = i;
}
}
return false;
}
// 源点,汇点,源点编号必须最小,汇点编号必须最大
int Dinic(int s, int t) {
int minCut, pos, maxFlow = 0;
int i, id = 0, u, v, now;
while(countLayer(s, t)) {
memset(vis, 0, sizeof(vis));
vis[s] = true; queue[id++] = s;
while(id) {
now = queue[id - 1];
if(now == t) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
if(G[u][v] < minCut) {
minCut = G[u][v];
pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(queue[id - 1] != pos)
vis[queue[--id]] = false;
} else {
for(i = s; i <= t; ++i) {
if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) {
vis[i] = 1; queue[id++] = i; break;
}
}
if(i > t) --id;
}
}
}
return maxFlow;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ1149 PIGS 【最大流量】的更多相关文章

  1. POJ1149 PIGS 【最大流 + 构图】

    题目链接:http://poj.org/problem?id=1149 PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  2. POJ1149 PIGS [最大流 建图]

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

  3. POJ1149 PIGS (网络流)

                                                                             PIGS Time Limit: 1000MS   M ...

  4. POJ1149 PIGS

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

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

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

  6. 题解 POJ1149 Pigs

    先翻译一下吧(题面可以在原OJ上找) Mirko在一个由M个锁着的猪舍组成的养猪场工作,Mirko无法解锁任何猪舍,因为他没有钥匙.客户纷纷来到农场.他们每个人都有一些猪舍的钥匙,并想购买一定数量的猪 ...

  7. POJ1149 PIGS(最大流)

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

  8. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  9. [BZOJ1280][POJ1149]Emmy卖猪pigs

    [BZOJ1280][POJ1149]Emmy卖猪pigs 试题描述 Emmy在一个养猪场工作.这个养猪场有 \(M\) 个锁着的猪圈,但Emmy并没有钥匙.顾客会到养猪场来买猪,一个接着一个.每一位 ...

随机推荐

  1. Java数据结构系列之——栈(2):栈的链式存储结构及其操作

    package Stack; import LinkList.SinglyLinkList; public class LinkListStack { private SinglyLinkList&l ...

  2. Bootstrap网站模板

    根据一篇文章,我再想想写下,无意义,他决定收手. 或者直接做一个简单的基本的模板它 主要知识点包含栅格系统.响应式图片.导航条(固定在顶部和底部).搜索框等等 详细每一个知识点不再赘述,參考Boots ...

  3. 64地点 Windows 8/7 根据系统 32地点PLSQL 耦合 64 地点 Oracle 11g

    64地点 Windows 8/7 根据系统 32地点PL/SQL 耦合 64 地点 Oracle 11g     说明:安装后Oracle的 oci.dll 是64位的,而32位应用程序 PL/SQL ...

  4. Delphi F11 全屏

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. SICP 1.20经验

    1.20 两者之间的主要区别是,使我们明白的操作顺序. 网上找一些答案,都死了扩大. 我们所从事的IT的. 展开搞死人IT实践. 首先考虑应用程序 我们得到 gcd(206, 40) -> gc ...

  6. Sybase Unwired Platform(SUP) 经常使用资源整理(不断更新中)

    提示:建议刚開始学习的人看三个东西,详见以下的详细内容.然后再去看论坛,官方技术支持站点等资源. SUP移动开发平台 中文视频讲座 SUP入门讲座(Wang Jun) SUP系列学习笔记 SUP实验 ...

  7. 8 shell命令之find

    find命令,像cd一样经常使用.只是可能大多数时间仅仅要那么一两个參数就足够使用了.或者说,勉强够用了.可是当我们主动的去翻看一下find的手冊,会发现原来更实用的功能都没实用到. 本文结合自己的使 ...

  8. OpenStack Dashboard

    OpenStackDashboard 为管理员和普通用户提供了一个图形化管理界面.用户能够通过该界面訪问.分配或者自己主动化分配基于云的资源.可扩展的设计使得与第三方产品和服务融合变得非常easy,比 ...

  9. Cocos2d-x学习笔记(五岁以下儿童) 精灵两种方式播放动画

     这几天在看控件类,临时没有想好实际运用的方向.单纯的创建网上已经有非常多这方面的样例,我就不写了.接下来是学习精灵类.精灵类若是单独学习也是非常easy.于是我加了一些有关动画方面的知识点与精灵 ...

  10. Visual Studio 2010 将网站直接发布到远程站点

    原文:Visual Studio 2010 将网站直接发布到远程站点 这次说下如何将web应用程序直接发布到IIS服务器站点!!! 问题的由来 本人每天要发布更新的程序,所以每次更新的时候要做的工作: ...