您的位置:首页 > 其它

POJ 1149 PIGS (最大流建图)

2017-09-30 09:32 411 查看
题目原文:

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.

题目大意:题目有一点不太好理解,需要注意一下,就是说所有的人是按照次序到来的,每个人都只能打开自己有钥匙的猪圈,然后那个主人就只能将这些打开的猪圈中的猪随意安置,之后再将猪圈关闭,问最多能卖掉多少猪?

解题思路

关键在于建图,新建一个超级源点和一个汇点,把每个顾客当成一个节点,然后源点会向那些第一次打开一个猪圈的人发出一条容量为猪圈中猪的个数的边,然后这个人会向下一次打开同一个猪圈的人发出一条容量为+∞的边,最后所有顾客会向汇点发出一条容量为想要购买的数量的边。最后跑最大流即可。

AC代码:

/*
* @Author: wchhlbt
* @Last Modified time: 2017-09-29
*/
//#include <bits/stdc++.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>

#define inf 0x3f3f3f3f
#define pb push_back
#define AA first
#define BB second
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;

typedef pair<int, int> P;
typedef long long ll ;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);
inline int read(){ int num; scanf("%d",&num); return num;}
const int maxn = 1007;

//最大流Ford-Fulkerson方法
/*
多组数据需要每次调用init
*/
struct edge{
int to,cap,rev;
//rev存储了反向边的编号
edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {}
};

vector<edge> G[maxn];
bool used[maxn];

void add(int from, int to, int cap)
{
G[from].pb(( edge(to, cap, G[to].size()) ));
G[to].pb( (edge(from,0,G[from].size()-1) ));
}

int dfs(int v, int t, int f)
{
if(v==t) return f;
used[v] = true;
for(int i = 0; i<G[v].size(); i++){
edge &e = G[v][i];
if(!used[e.to] && e.cap > 0){
int d = dfs(e.to, t, min(f,e.cap));
if(d>0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}

int maxflow(int s, int t)
{
int flow = 0;
while(1){
memset(used, 0, sizeof(used));
int f = dfs( s, t, inf);
if(f==0) return flow;
flow += f;
}
}

void init()
{
for(int i = 0; i<maxn; i++)
G[i].clear();
}

int a[maxn];//记录每一个猪圈中猪的数量
int last[maxn];//记录每个猪圈的上一个顾客
int b[maxn];//记录每个顾客想要买多少猪

int main()
{
int m = read(); int n = read();
for(int i = 1; i<=m; i++)
a[i] = read();
for(int i = 1; i<=n; i++){
int num = read();
int sum = 0;
for(int j = 1; j<=num; j++){
int t = read();
if(last[t]==0){//如果之前没人打开过这个猪圈
last[t] = i;
sum += a[t];
}
else{
add(last[t],i,inf);
last[t] = i;
}
}
b[i] = read();
add(0,i,sum);
add(i,n+1,b[i]);
}
printf("%d\n",maxflow(0,n+1));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: