POJ1149_PIGS(网络流/EK)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15721 | Accepted: 7021 |
Description
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 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
Sample Input
3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6
Sample Output
7
Source
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#define inf 99999999
#define M 10100
#define N 1100
using namespace std;
int pigh[M],edge[N][N],p[N],a[N],pre[N],m,n,flow;
queue<int >Q;
void ek()
{
while(1)
{
while(!Q.empty())Q.pop();
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
a[0]=inf;
Q.push(0);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=0;v<=n+1;v++)
{
if(!a[v]&&edge[u][v]>0)
{
a[v]=min(a[u],edge[u][v]);
p[v]=u;
Q.push(v);
}
}
if(a[n+1])break;
}
if(!a[n+1])break;
for(int u=n+1;u!=0;u=p[u])
{
edge[p[u]][u]-=a[n+1];
edge[u][p[u]]+=a[n+1];
}
flow+=a[n+1];
}
}
int main()
{
int i,j,k,u,b;
while(~scanf("%d%d",&m,&n))
{
flow=0;
memset(pigh,0,sizeof(pigh));
memset(edge,0,sizeof(edge));
memset(pre,0,sizeof(pre));
for(i=1; i<=m; i++)
scanf("%d",&pigh[i]);
for(i=1; i<=n; i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&u);
if(!pre[u])
{
edge[pre[u]][i]+=pigh[u];
pre[u]=i;
}
else
{
edge[pre[u]][i]=inf;
pre[u]=i;
}
}
scanf("%d",&b);
edge[i][n+1]+=b;
}
ek();
// for(i=0;i<=n+1;i++)
// {
// for(j=0;j<=n+1;j++)
// {
// cout<<edge[i][j]<<" ";
// }
// cout<<endl;
// }
printf("%d\n",flow);
}
return 0;
}
POJ1149_PIGS(网络流/EK)的更多相关文章
- 网络流EK
#include <iostream> #include <queue> #include <string.h> #define MAX 302 using nam ...
- POJ 1459 网络流 EK算法
题意: 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条:(0,1)20 (1,0) ...
- 初涉网络流[EK&dinic]
主要还是板子 Edmonds-Karp 从S开始bfs,直到找到一条到达T的路径后将该路径增广,并重复这一过程. 在处理过程中,为了应对“找到的一条路径把其他路径堵塞”的情况,采用了建反向弧的方式来实 ...
- 最大网络流 EK 算法
网络流是什么类型的问题,看一道题目你就知道了 点击打开链接 . 默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念.比较好!一个寄出的网络最大流.EK算法写的. 这是一幅网络,求S ...
- 网络流EK算法模板
\(EK\)算法的思想就是每一次找一条增广路进行增广. 注意几个点: 存图时\(head\)数组要设为\(-1\). 存图的代码是这样的: inline void add(int u, int v, ...
- HDU1532 Drainage Ditches 网络流EK算法
Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...
- 2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK
题源:https://nanti.jisuanke.com/t/11215 分析:这题是一个比较经典的网络流模型.把中间节点当做源,两端节点当做汇,对节点进行拆点,做一个流量为 22 的流即可. 吐槽 ...
- 网络流 ek
hdu3549 求最大流果题 ek算法 先bfs出一条流 然后通过不断地添加增广路 得到最大流(证明在算法书上都有) 增加了一个流 就加反向边 允许程序通过走方向边的方式进行“回滚” i^1 = i+ ...
- POJ-3436 ACM Computer Factory(网络流EK)
As you know, all the computers used for ACM contests must be identical, so the participants compete ...
随机推荐
- ASP.NET CORE-Info:TechEmpower最新一轮的性能测试出炉,ASP.NET Core依旧表现不俗
ylbtech-ASP.NET CORE-Info:TechEmpower最新一轮的性能测试出炉,ASP.NET Core依旧表现不俗 1.返回顶部 1. TechEmpower在10月30发布最新一 ...
- PHP面试 PHP基础知识 三(运算符)
PHP运算符 PHP的运算符的错误控制符@ PHP支持一个错误运算符:@.当将其放在一个PHP表达式之前,该表达式可能产生的任何错误信息都将会被忽略掉. PHP运算符 运算符的优先级 着重记忆运算符 ...
- Mina(一)
配置log4j注意事项: Log4J 1.2 users: slf4j-api.jar, slf4j-log4j12.jar, and Log4J 1.2.x slf4j-log4j*.jar要对应 ...
- Jeecg集成Swagger-ui
<context:component-scan base-package="springfox"/> <bean class="org.jeecgfra ...
- 1、Appium Desktop介绍
Appium Desktop是一款适用于Mac,Windows和Linux的开源应用程序,它以美观而灵活的用户界面为您提供Appium自动化服务器的强大功能.它是几个Appium相关工具的组合: Ap ...
- 022_IO流
对象流 // FileInput.FileOutputStream(节点流)ObjectInputStreamObjectOutputStream 序列化 把内存的数据信息永久的保存在硬盘中,这个过程 ...
- C++子类父类构造函数的关系
在C++中子类继承和调用父类的构造函数方法 构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法).因此,在创建子类对象 ...
- [已解决]报错This event loop is already running
安装nest_asyncio pip install nest_asyncio 导入并调用 import nest_asyncio nest_asyncio.apply()
- 为Python终端提供持久性历史记录
有没有办法告诉交互式Python shell在会话之间保留其执行命令的历史记录? 当会话正在运行时,在执行命令之后,我可以向上箭头并访问所述命令,我只是想知道是否有某种方法可以保存这些命令,直到下次我 ...
- springboot中引用配置文件中的参数
首先可以看到这是做微信登陆时候的配置,一般不会写死都是通过配置文件获取,所以,记载配置文件中 那么怎么引用呢: 可以看到直接注入的方式就可以引用了,所以看下面: 进行页面跳转,并且带有参数的, 使用m ...