PIGS

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 18209 Accepted: 8277

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

Croatia OI 2002 Final Exam - First day

题意:有不同的猪圈,买家手里有着猪圈的钥匙,拥有那个猪圈的钥匙就可以买这个猪圈的猪,怎样安排买猪才能卖出更多的猪;

#include <map>
#include <list>
#include <climits>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int Max = 100000; int cus[110][110];//图的容量 int flow[110][110];//图的流量 int num[1100];//猪的数量 int pre[1100];//前一个买家 int n,m,data; int t,s; int ford()//一般增广路算法
{
int minflow[110];
queue<int >Q;
memset(flow,0,sizeof(flow));
minflow[0] = INF;
t=n+1;
s=0;
while(1)
{
for(int i=0;i<=t;i++)
{
pre[i]=-2;
}
while(!Q.empty())
{
Q.pop();
}
pre[0]=-1;
Q.push(0);
while(!Q.empty()&&pre[t]==-2)//BFS找增广路
{
int v=Q.front();
int p;
Q.pop();
for(int i=0;i<=t;i++)
{
if(pre[i]==-2&&(p=cus[v][i]-flow[v][i]))
{
pre[i]=v;
Q.push(i);
minflow[i]=min(minflow[v],p);
}
}
}
if(pre[t]==-2)//如果找不到汇点,则说明图中不存在增广路,算法结束
{
break;
}
for(int i=pre[t],j=t;i!=-1;j=i,i=pre[i])//回溯进行增广
{
flow[i][j] +=minflow[t];
flow[j][i]-=minflow[t];
}
}
int sum=0;
for(int i=0;i<t;i++)
{
sum+=flow[i][t];//每个点到汇点的总和为最大流
}
return sum;
} int main()
{
while(~scanf("%d %d",&m,&n))
{
memset(pre,-1,sizeof(pre));
memset(cus,0,sizeof(cus));
for(int i=1;i<=m;i++)
{
scanf("%d",&num[i]);
}
int s;
for(int i=1;i<=n;i++)
{
scanf("%d",&data);
for(int j=0;j<data;j++)
{
scanf("%d",&s);
if(pre[s]==-1)//如果这个猪圈之前没有人,则这个人是第一个人,所以与源点之间的容量为猪圈的猪的数量
{
cus[0][i]+=num[s];
}
else
{
cus[pre[s]][i]=INF;//如果已经有人买了,所以他必须在这个人之后买,但两人之间又没有关系,所以容量为INF(无穷大)
}
pre[s]=i;
}
scanf("%d",&cus[i][n+1]);//到汇点的容量为每个人想买猪的数量
}
printf("%d\n",ford());
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏的更多相关文章

  1. *** glibc detected *** malloc(): memory corruption 分类: C/C++ Linux 2015-05-14 09:22 37人阅读 评论(0) 收藏

    *** glibc detected *** malloc(): memory corruption: 0x09eab988 *** 发现是由于memset越界写引起的. 在Linux Server上 ...

  2. 修改MS SQL忽略大小写 分类: SQL Server 数据库 2015-06-19 09:18 34人阅读 评论(0) 收藏

    第一步:数据库->属性->选项->限制访问:SINGLE_USER 第二步:ALTER DATABASE [数据库名称] collate Chinese_PRC_CI_AI 第三步: ...

  3. 本周博客--WinForm线程初步 2014-10-31 09:15 54人阅读 评论(0) 收藏

    嗯,WinForm作为一个比较程序的微软的技术已经为我们服务了很久很久,在技术不断更新的今天WinForm虽显老态龙钟但是在客户端快速开发中,它的地位还是无可取代的 今天就来分享一下WinForm中的 ...

  4. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

  5. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  6. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...

  7. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  8. Poj 2528 Mayor's posters 分类: Brush Mode 2014-07-23 09:12 84人阅读 评论(0) 收藏

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40570   Accepted: 11798 ...

  9. Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9557   Accepted: 3187 De ...

随机推荐

  1. Java基础之访问文件与目录——创建目录(CreatingDirectories)

    控制台程序,使用两种方法来创建目录. import java.nio.file.*; import java.io.IOException; public class CreatingDirector ...

  2. 《30天自制操作系统》08_day_学习笔记

    harib05a: 鼠标解读(01)P145 前一天已经让鼠标成功接收数据了,这些数据是什么意思? 笔者在这一部分来解读数据:让鼠标动起来啊,停在那不动有什么意思啊! 前面已经知道,鼠标每一次动作都是 ...

  3. Python高频技巧总结[基础篇]

    0. 概要说明 python应用最多的场景还是web快速开发.爬虫.自动化运维:简单网站.自动Fuzz脚本.收发邮件脚本.简单验证码识别脚本. 爬虫在开发过程中也有很多复用的过程,这里总结一下,以后也 ...

  4. Java基础(5):试用Array类对数据进行操作(Sort和toString方法)

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等 1. 排序 语法:  Arrays.sort(数 ...

  5. 如何把Eclipse工程import Exprot到Android Studio

    http://jingyan.baidu.com/article/b87fe19e9e209f5218356808.html

  6. sql 存储过程 分页

    ALTER PROCEDURE [dbo].[BrokerToLenderDataShow2]@Where VARCHAR(200), --查询条件 不含'where'字符,如id>10 and ...

  7. 夺命雷公狗---DEDECMS----9dedecms单标签

    我们这一节课开始将dedecms的标签了,dedecms里面的标签分好多个的,我们先来看下他的标签长得啥样的先: 随便点击一个修改即可见到标签了: 这里面上面的大文本框里面有标签的用法下面有参数的说明 ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON FillUp2

    zw版[转发·台湾nvp系列Delphi例程]HALCON FillUp2 procedure TForm1.Button1Click(Sender: TObject);var op : HOpera ...

  9. HTML5,超级链接

    <a href="http://h123.date">预算控制系统</a><<br><a href="2.html&quo ...

  10. 向已写好的多行插入sql语句中添加字段和值

    #region 添加支款方式--向已写好的多行插入sql语句中添加字段和值 public int A_ZhifuFS(int diqu) { ; string strData = @"SEL ...