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. mysql的binlog安全删除

    理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越来越多的binlog,磁盘被吃掉了不少.可以 ...

  2. Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-3 显示一个动态的熊猫

    一个静态的熊猫明显不能满足我们的欲望,接下来我们就让熊猫跑起来.序列帧动画的原理就是不停的切换不同的图片.当我们将一张一张的切换Panda类的跑动文理后,熊猫就跑起来了.那么首先我们需要一个数组常量来 ...

  3. C++初学者 const使用详解

     关于C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,参考了康建东兄的const使用详解一文,对其中进行了一些补充,写下了本文. 1. const常量 如: cons ...

  4. TF255466: Team Foundation Server 的配置过程无法继续。以前的更新或安装需要重

    在验证是否可以安装 SharePoint 时的提示,Error [ System Checks ] TF255466: The configuration process for Team Found ...

  5. 解决无法连接到visual studio开发服务器的问题

    今天vs抽风,调试网站出现下图那样: 然后我开始百度搜索 "无法连接到visual studio开发服务器" 出现很多文章: 打开一篇问题,看了里面的内容, 那个解决办法也不是最好 ...

  6. sql 索引创建

    --格式 --CREATE [索引类型] INDEX 索引名称--ON 表名(列名)--WITH FILLFACTOR = 填充因子值0~100--GO ----------------------- ...

  7. rails控制台进入

    数据库控制台: rails db .help查看可使用的命令 rails控制台 rails c 普通ruby控制台: irb

  8. Oracle数据库归档模式的切换及其相关操作详解

    SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> ...

  9. 清华大学出版社即将推出的又一本挂羊头卖狗肉的劣书 《C语言入门1.2.3—一个老鸟的C语言学习心得》

    http://www.tup.com.cn/book/showbook.asp?CPBH=051892-01

  10. Oracle的导入导出

    写批处理文件的基础知识 http://blog.chinaunix.net/uid-20672257-id-3056546.html http://wenku.baidu.com/view/db9c5 ...