PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15721   Accepted: 7021

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

解题报告
昨天開始学网络流,这是第一题网络流建图的题。
题目意思:
养猪场M个猪圈。每一个猪圈都上锁,主人又没有钥匙。N个顾客买猪。且每一个顾客有一些猪圈的钥匙(这是什么情况,主人没有钥匙。反而买主有钥匙。sad...)
一天,要到养猪场买猪的顾客都会提前告诉养猪场主人。包含拥有的钥匙。买几头猪。养猪场主人能够安排销售计划使得卖出去的猪数目最大。
每当顾客来了。会把他拥有钥匙的猪圈全都打开,养猪场主人挑一些猪买出去,养猪场主人还能够又一次分配被打开猪圈的猪。

猪圈能够容纳猪的数量不限。

思路:
由于一開始猪圈是上锁的。所以把顾客其中转站,另设两节点,源点和汇点。
源点和每一个猪圈的第一个顾客连边。边的权值是猪圈里的猪的数目。
顾客j紧跟着顾客i打开某猪圈,则<i,j>的权值是+oo,表示假设顾客j在顾客i之后打开猪圈,主人能够跟据顾客j的需求把其它猪圈的猪赶到该猪圈,这样顾客j就能够买到尽可能多的猪。
每一个顾客和汇点相连。边权是每一个顾客的需求量。

#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)的更多相关文章

  1. 网络流EK

    #include <iostream> #include <queue> #include <string.h> #define MAX 302 using nam ...

  2. 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) ...

  3. 初涉网络流[EK&dinic]

    主要还是板子 Edmonds-Karp 从S开始bfs,直到找到一条到达T的路径后将该路径增广,并重复这一过程. 在处理过程中,为了应对“找到的一条路径把其他路径堵塞”的情况,采用了建反向弧的方式来实 ...

  4. 最大网络流 EK 算法

    网络流是什么类型的问题,看一道题目你就知道了 点击打开链接 . 默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念.比较好!一个寄出的网络最大流.EK算法写的. 这是一幅网络,求S   ...

  5. 网络流EK算法模板

    \(EK\)算法的思想就是每一次找一条增广路进行增广. 注意几个点: 存图时\(head\)数组要设为\(-1\). 存图的代码是这样的: inline void add(int u, int v, ...

  6. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  7. 2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK

    题源:https://nanti.jisuanke.com/t/11215 分析:这题是一个比较经典的网络流模型.把中间节点当做源,两端节点当做汇,对节点进行拆点,做一个流量为 22 的流即可. 吐槽 ...

  8. 网络流 ek

    hdu3549 求最大流果题 ek算法 先bfs出一条流 然后通过不断地添加增广路 得到最大流(证明在算法书上都有) 增加了一个流 就加反向边 允许程序通过走方向边的方式进行“回滚” i^1 = i+ ...

  9. POJ-3436 ACM Computer Factory(网络流EK)

    As you know, all the computers used for ACM contests must be identical, so the participants compete ...

随机推荐

  1. 转-C++之手写strcpy

    转自:strcpy函数的实现 知strcpy函数的原型是: char *strcpy(char *dst, const char *src); 实现strcpy函数 解释为什么要返回char * 假如 ...

  2. XML 扩展部分

    引入命名空间 xmlns DTD缺点 1.不支持命名空间 2.支持的数据类型很少 3.DTD不可扩展 4.DTD不遵循XML规范 DTD的优点 简洁 schema 通过schema来解决DTD的不足 ...

  3. 拾遗:Vim 批量删除匹配到的行

    删除包含特定字符的行 g/pattern/d (全局删除匹配行) ,5g/pattern/d (删除第1-5行里的匹配行) 删除不包含指定字符的行 v/pattern/d g!/pattern/d ( ...

  4. 【转载】Jmeter业务请求比例1

    ps:文章转自订阅号“测试那点事儿”,链接:https://mp.weixin.qq.com/s/qVD4iNO0QqRIwAIq9_E_Kw 在进行综合场景压测时,由于不同的请求,要求所占比例不同, ...

  5. log4j.properties的详细配置

    log4j.properties的详细配置 log4j.properties的maven配置 <dependency> <groupId>org.scala-lang</ ...

  6. idea plugin 进度条

    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Switching Env") { @Ove ...

  7. 高级UI晋升之View渲染机制(二)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...

  8. C# 设计模式 (一)

    学习来自<大话设计模式>有兴趣可以研究一下 一.简单工厂模式 原理 子类对象可以赋值给父类对象.同一个父类对象的子类,通过switch语句new出来(当然用if也可以但那样判断就多了起来) ...

  9. VS系列远程调试

    其实很多时候,远程调试的话,我还是更喜欢用WinDBG的, 首先,可以练习WinDBG的使用手段, 其次,可以增加WinDBG的熟练度, 最重要的,WinDBG在内核调试部分很常用,我也很喜欢它,所以 ...

  10. 组合,模板,bolck块

    如果前面和后面的变量名相同,则后面的变量名会覆盖前面的变量名 模板可以多次使用,只需要将is指定template的name,就可以重复使用该模板,只需要将不同的item值赋值给data就可以实现. 可 ...