当前位置:网站首页>Poj1149 pigs [maximum flow]
Poj1149 pigs [maximum flow]
2022-07-06 19:50:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
PIGS
Time Limit: 1000MS | Memory Limit: 10000K | |
|---|---|---|
Total Submissions: 16555 | Accepted: 7416 |
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 6Sample Output
7Source
Croatia OI 2002 Final Exam – First day
The main idea of the topic Mirko Keep some pigs Pigs are kept in some pigsty The pigsty is locked He doesn't have a key himself ( Khan, ) Only customers who want to buy pigs have keys Customers come in turn Every customer will use his key to open some pigsty buy Walk some pigs Then lock it up. Before locking Mirko There is a chance to allocate these opened pigpens again Pig of Now give the number of pigs in each pigsty at the beginning All the keys of every customer And the number of pigs to buy ask Mirko How many pigs can you sell at most
Answer key : For the first buyer of every pigsty , Add a source point to this person's side , The right is the number of pigs in this pigsty , For later people who want to buy the pigsty . Join a person who is the first to buy the pigsty to his side . take a temporary measure inf, Then join everyone to one side of the meeting point , The weight is the number of pigs that the person wants to buy . thus , The composition is finished .
#include <stdio.h>
#include <string.h>
#define inf 0x3fffffff
#define maxn 110
#define maxm 1002
int pig[maxm], m, n, sink;
int G[maxn][maxn], queue[maxn];
bool vis[maxn]; int Layer[maxn];
bool countLayer() {
memset(Layer, 0, sizeof(Layer));
int id = 0, front = 0, now, i;
Layer[0] = 1; queue[id++] = 0;
while(front < id) {
now = queue[front++];
for(i = 0; i <= sink; ++i)
if(G[now][i] && !Layer[i]) {
Layer[i] = Layer[now] + 1;
if(i == sink) return true;
else queue[id++] = i;
}
}
return false;
}
int Dinic() {
int minCut, pos, maxFlow = 0;
int i, id = 0, u, v, now;
while(countLayer()) {
memset(vis, 0, sizeof(vis));
vis[0] = 1; queue[id++] = 0;
while(id) {
now = queue[id - 1];
if(now == sink) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
if(G[u][v] < minCut) {
minCut = G[u][v];
pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(queue[id - 1] != pos)
vis[queue[--id]] = 0;
} else {
for(i = 0; i <= sink; ++i) {
if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) {
vis[i] = 1; queue[id++] = i; break;
}
}
if(i > sink) --id;
}
}
}
return maxFlow;
}
int main() {
//freopen("stdin.txt", "r", stdin);
int i, keys, num;
while(scanf("%d%d", &m, &n) == 2) {
sink = n + 1;
for(i = 1; i <= m; ++i)
scanf("%d", &pig[i]);
memset(G, 0, sizeof(G));
for(i = 1; i <= n; ++i) {
scanf("%d", &keys);
while(keys--) {
scanf("%d", &num);
if(pig[num] >= 0) {
G[0][i] += pig[num]; // 0 is source
pig[num] = -i; // Here is the mark number num The first person to connect the pigsty
} else G[-pig[num]][i] = inf;
}
scanf("%d", &G[i][sink]);
}
printf("%d\n", Dinic());
}
return 0;
}2015.4.20
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
int G[maxn][maxn], M, N, S, T;
int pigHouse[maxn*10];
int Dinic(int s, int t);
void getMap()
{
memset(G, 0, sizeof(G));
S = 0; T = N + 1;
int i, j, K, pos;
for (i = 1; i <= M; ++i)
scanf("%d", &pigHouse[i]);
for (i = 1; i <= N; ++i) {
scanf("%d", &K);
while (K--) {
scanf("%d", &pos);
if (pigHouse[pos] >= 0) {
G[S][i] += pigHouse[pos];
pigHouse[pos] = -i;
} else {
G[-pigHouse[pos]][i] = inf;
}
}
scanf("%d", &G[i][T]);
}
}
void solve()
{
cout << Dinic(S, T) << endl;
}
int main()
{
while (cin >> M >> N) {
getMap();
solve();
}
return 0;
}
int queue[maxn];
bool vis[maxn]; int Layer[maxn];
bool countLayer(int s, int t) {
memset(Layer, 0, sizeof(Layer));
int id = 0, front = 0, now, i;
Layer[s] = 1; queue[id++] = s;
while(front < id) {
now = queue[front++];
for(i = s; i <= t; ++i)
if(G[now][i] && !Layer[i]) {
Layer[i] = Layer[now] + 1;
if(i == t) return true;
else queue[id++] = i;
}
}
return false;
}
// Source point , Confluence , The source point number must be the smallest , The sink number must be the largest
int Dinic(int s, int t) {
int minCut, pos, maxFlow = 0;
int i, id = 0, u, v, now;
while(countLayer(s, t)) {
memset(vis, 0, sizeof(vis));
vis[s] = true; queue[id++] = s;
while(id) {
now = queue[id - 1];
if(now == t) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
if(G[u][v] < minCut) {
minCut = G[u][v];
pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(queue[id - 1] != pos)
vis[queue[--id]] = false;
} else {
for(i = s; i <= t; ++i) {
if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) {
vis[i] = 1; queue[id++] = i; break;
}
}
if(i > t) --id;
}
}
}
return maxFlow;
}Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117136.html Link to the original text :https://javaforall.cn
边栏推荐
- POJ1149 PIGS 【最大流量】
- [translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
- 范式的数据库具体解释
- 腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
- Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
- Database specific interpretation of paradigm
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- 腾讯安卓开发面试,android开发的基础知识
- Recursive implementation of department tree
- 【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
猜你喜欢

How to access localhost:8000 by mobile phone

Spark foundation -scala

Tencent Android interview must ask, 10 years of Android development experience

Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
![[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.](/img/09/106adc222c06cbd2f4f66cf475cce2.jpg)
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.

Information System Project Manager - Chapter VIII project quality management

Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?

Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
腾讯T2大牛亲自讲解,跳槽薪资翻倍
随机推荐
在解决了 2961 个用户反馈后,我做出了这样的改变...
Classic 100 questions of algorithm interview, the latest career planning of Android programmers
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
Transformer model (pytorch code explanation)
121. The best time to buy and sell stocks
OceanBase社区版之OBD方式部署方式单机安装
Tensorflow2.0 自定义训练的方式求解函数系数
部门树递归实现
Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
leetcode先刷_Maximum Subarray
【翻译】供应链安全项目in-toto移至CNCF孵化器
学习探索-无缝轮播图
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
凤凰架构3——事务处理
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
从sparse.csc.csr_matrix生成邻接矩阵
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
激进技术派 vs 项目保守派的微服务架构之争
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate