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. iOS中scrollview是否要回弹

    1. @property(nonatomic) BOOL bounces //当滚动到内容边缘是否发生反弹,default is YES.2. @property(nonatomic) BOOL al ...

  2. PostgreSQL 同步复制(1master+2standby)

    OS: Red Hat Enterprise Linux Server release 6.5 (Santiago) PostgreSQL: postgresql-9.4.5.tar.bz2 mast ...

  3. NGINX反向代理

                                                    Nginx反向代理                                           ...

  4. SQL查询一个表的总记录数的方法

    一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指定列 SQL>SELECT empmo, ...

  5. JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面

    将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...

  6. Linux centOS7 下安装mysql5.7.10

    1:下载二进制安装包 http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz 2:解压到 ...

  7. 高并发应用中客户端等待、响应时间的推算,及RT/QPS概念辨析

    高并发应用中客户端等待.响应时间的推算,及RT/QPS概念辨析 对于一个网站,已知服务端的服务线程数和处理单个请求所需的时间时,该如何算出高并发时用户从点击链接到收到响应的时间?注意这个时间并不等于服 ...

  8. android 应用架构随笔三(ListView)

    import java.util.ArrayList; import java.util.List; import com.heima.googleplay.holder.BaseHolder; im ...

  9. [转]AngularJS的$resource

    转自:http://blog.csdn.net/violet_day/article/details/17403207 $http $http服务是基于$q服务的,提供了promise封装,它接受一个 ...

  10. api(接口)文档管理工具

    api(接口)文档管理工具 欢迎光临:博之阅API管理平台  ,做为一个app开发者,还没有用到api管理工具,你就OUT了 点击进入:程序员精华博客大全