PIGS
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 |
[Submit] [Go Back] [Status] [Discuss]
题意: 有 M 个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依次来了 N 个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。
分析: 此题是网络流,网络流里经典的构图题。将顾客看作除源和汇以外的节点,源和每个猪圈的第一个顾客连边,边的权是开始时猪圈中猪的数目,若源和某个节点之间有重边,则将权合并,每个猪圈的前后相邻两顾客之间连边,由前一个顾客指向后一个顾客。每个顾客和汇之间连边,边的权是顾客所希望购买的猪的数目。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define maxn 1600
#define maxm 100086+10
const int mod = 1e9+7;
#define eps 1e-7
#define PI acos(-1.0)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//const int INF=1e9;
struct Edge{
int from,to,cap,flow;
Edge(int f = 0, int t = 0, int c = 0, int fl = 0):from(f),to(t),cap(c),flow(fl){}
};
int n,m,s,t;
int S,T;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n)
{
edges.clear();
rep(i,n+3) G[i].clear();
}
int AddEdge(int from,int to,int cap){
edges.push_back(Edge(from, to, cap, 0)); //返回的是这条的编号
edges.push_back(Edge(to, from, 0, 0));
int p = edges.size();
G[from].push_back(p - 2);
G[to].push_back(p - 1);
return p - 2;
}
bool BFS(int s,int t){
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while(!q.empty()){
int x = q.front(); q.pop();
for(int i = 0;i<(int)G[x].size();i++){
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1; d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a,int t){
if(x == t || a == 0) return a;
int flow = 0,f;
for(int &i = cur[x];i < (int)G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(d[x]+1 == d[e.to] &&(f = DFS(e.to,min(a,e.cap-e.flow),t))>0){
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int maxflow(int s,int t){ //dinic
int flow = 0;
while(BFS(s,t)){
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF, t);
}
return flow;
}
vector<int> p[maxn]; //p[i][j]表示第i头猪的第j个顾客
int val[maxn];
int needs[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
while(~scanf("%d%d",&m,&n))
{
init(n+1);
rep(i,m+1) p[i].clear();
s = n + 1;
t = s + 1;
rep1(i,m)
scanf("%d",&val[i]);
rep(i,n)
{
int k;
cin>>k;
while(k--)
{
int x;
cin>>x;
p[x].push_back(i);
}
cin>>needs[i];
}
/*rep1(i,m)
{
int sz = p[i].size();
rep(j,sz) cout<<p[i][j]<<' ';
cout<<endl;
}*/
rep(i,n) AddEdge(i,t,needs[i]);
rep1(i,m)
{
int sz = p[i].size();
if(sz != 0) AddEdge(s,p[i][0],val[i]);
for(int j = 1;j < sz;j++) AddEdge(p[i][j-1],p[i][j],INF);
}
cout<<maxflow(s,t)<<endl;
}
return 0;
}