PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20738   Accepted: 9481

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

题目链接:POJ 1149

题意:有M个猪圈,每一个猪圈有初始数量的猪,所有猪都是一样的,有N个人,每一个人有K把钥匙可以打开K个猪圈,对与被打开的猪圈可以进行重新分配猪的数量,每个人又有要买的猪的数量,每一个人按照输入的顺序进来购买,求最后所有人能买到的猪的最大个数。

解题思路:首先肯定是在源点连到每一个猪圈,容量为该猪圈的初始数量;然后把人连到汇点,容量为人要购买的头数,由于当前买的时候可能对应猪圈已经被重新分配过,因此不是从源点连过来,而是从上一个拥有这个猪圈的钥匙的人连过来一条容量为无穷大的边;若你是第一个开猪圈的人则要从猪圈连过来一条容量为无穷大的边。然后求源点到汇点的最大流即可。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1110;
const int M = 110 + N + 110 * N + N;
struct edge
{
int to, nxt, cap;
edge(int To = 0, int Nxt = 0, int Cap = 0): to(To), nxt(Nxt), cap(Cap) {}
};
edge E[M << 1];
int head[N], d[N], tot;
int opened[N]; void init()
{
CLR(head, -1);
tot = 0;
CLR(opened, 0);
}
inline void add(int s, int t, int cap)
{
E[tot] = edge(t, head[s], cap);
head[s] = tot++;
E[tot] = edge(s, head[t], 0);
head[t] = tot++;
}
int bfs(int s, int t)
{
CLR(d, -1);
d[s] = 0;
queue<int>q;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (d[v] == -1 && E[i].cap > 0)
{
d[v] = d[u] + 1;
if (v == t)
return 1;
q.push(v);
}
}
}
return ~d[t];
}
int dfs(int s, int t, int f)
{
if (s == t || !f)
return f;
int ret = 0;
for (int i = head[s]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (d[v] == d[s] + 1 && E[i].cap > 0)
{
int df = dfs(v, t, min(f, E[i].cap));
if (df > 0)
{
E[i].cap -= df;
E[i ^ 1].cap += df;
ret += df;
f -= df;
if (!f)
break;
}
}
}
if (!ret)
d[s] = -1;
return ret;
}
int Dinic(int s, int t)
{
int ret = 0;
while (bfs(s, t))
ret += dfs(s, t, INF);
return ret;
}
int main(void)
{
int m, n, b, c, i, j, k, id;
while (~scanf("%d%d", &m, &n))
{
init();
int S = 0, T = n + m + 1;
for (i = 1; i <= m; ++i)
{
scanf("%d", &c);
add(S, i, c);
}
for (i = 1; i <= n; ++i)
{
scanf("%d", &k);
for (j = 0; j < k; ++j)
{
scanf("%d", &id);
if (!opened[id])
add(id, m + i, INF);
else
add(opened[id], m + i, INF);
opened[id] = m + i;
}
scanf("%d", &b);
add(m + i, T, b);
}
printf("%d\n", Dinic(S, T));
}
return 0;
}

POJ 1149 PIGS(Dinic最大流)的更多相关文章

  1. poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新

    题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...

  2. POJ 1149 PIGS(最大流)

    Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock an ...

  3. poj 1149 PIGS【最大流】

    建图:s向所有猪圈的第一个顾客连流量为这个猪圈里住的数量,然后对于之后每个来这个猪圈的顾客,由他前一个顾客向他连边权为无穷的边,然后每个顾客向t连流量为这个顾客购买上限的边.然后跑最大流 #inclu ...

  4. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  5. poj 1149 PIGS(最大流经典构图)

    题目描述:迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的 ...

  6. POJ 1149 PIGS 【最大流】

    <题目链接> 题目大意:有一个养猪场,厂长没有钥匙,这个养猪场一共M个猪圈,N个顾客,每个顾客有一些猪圈的钥匙,每个顾客需要一些猪,问你厂长最多能卖多少猪?这里有个条件是,厂长可以在一个顾 ...

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

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

  8. 网络流(最大流):POJ 1149 PIGS

    PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. 64-bit integer(整数) ...

  9. POJ 1149 PIGS 建图,最大流

    题意: 你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开某一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 已打开的 ...

随机推荐

  1. Finite Encyclopedia of Integer Sequences(找规律)

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB提交: 375  解决: 91[提交] [状态] [讨论 ...

  2. 函数定义和调用 -------JavaScript

    本文摘要:http://www.liaoxuefeng.com/ 定义函数 在JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { ret ...

  3. java算法面试题:金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

    package com.swift; public class RenMingBi { private static final char[] data = new char[]{'零','壹','贰 ...

  4. 微信公众帐号开发之一(java)

    闲来没事,就记录一下微信公众平台的开发吧~ 其实微信公众平台开发没有想象中的那么困难,因为注册了微信公众平台帐号登录之后在开发者模式里有详细的文档,个人感觉介绍还是比较详细的. 微信公众平台订阅号和服 ...

  5. linux中搭建公网ftp服务器

    Linux搭建ftp服务器汇总整理 一.检查linux中是否已经安装vsftpd服务端软件 rpm -qa |grep vsftpd 二.卸载linux中的vsftpd服务端软件 rpm -e vsf ...

  6. url传参及重定向

    成功跳转$this -> success('提示语',跳转路径,返回的数据,时间,发送的 Header 信息)跳转失败$this -> error('提示语',跳转路径,返回的数据,时间, ...

  7. CMDB(资产管理系统) day1

    运维自动化最重要的就是标准化一切 自动化运维则支持以下功能: 1.OS的选择统一化,同一个项目使用同样的OS系统部署其所需要的各类软件.2.软件安装标准化,例如JAVA虚拟机,php,nginx,my ...

  8. centos7.3网络配置

    一.关闭NetworkManager 默认状态下最小化安装使用NetworkManager这个服务来控制联网的,但是这个配置在配置生产环境服务器时一般不会使用,而是使用系统自带的network服务,更 ...

  9. STM32串口——中断方式的一般配置方法

    #include "stm32f10x.h" /************************************************ 该程序讲解串口程序的一般配置方法: ...

  10. Codeforces Round #459 (Div. 2):B. Radio Station

    B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...