Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18051 | Accepted: 8206 |
Description
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
题意:迈克在一个养猪场工作,养猪场有M个猪圈,每个猪圈都有一定数量的猪且都上了锁。由于迈克没有钥匙,所以他不能打开任何一个猪圈。要买猪的顾客一个接一个的来到养猪场,每个顾客都有一些猪圈的钥匙,而且他们要买一定数量的猪。具体销售过程:当每个顾客到来时,他将那些他拥有钥匙的猪圈全部打开;迈克从这些猪圈中挑出一些猪卖给他们;如果迈克愿意,他可以重新分配这些被打开的猪圈中的猪;当顾客离开时,猪圈再次被锁上。注意:猪圈可容纳的猪的数量没有限制。
现在我们知道每个猪圈猪的数目、每个顾客手中拥有的全部钥匙以及他们要买的猪的数目,问你迈克一天最多可以买出多少头猪。
经典建图:
一:我们选取0为超级源点,N+1为超级汇点;
二:对于每个猪圈第一个到来的顾客i,从源点引一条到i的边,权值为猪圈猪的数量,因为迈克可以从该猪圈里面取出任意数量的猪给该顾客(满足顾客需求的前提下);
三:对于每个猪圈,若它依次到来的顾客为x、y、z(假设的啊,可能顾客多于三个也可能少于三个),我们需要连接x->y
和y->z,且边权为INF无穷大。因为在打开这个猪圈后,迈克可能会根据需求把其它猪圈的猪分配到该猪圈。只要他想,猪的分配数当然可以任意。
四:每个顾客和汇点N+1连边,权值为该顾客的想买猪的数量,因为最终到汇点的流量才是结果。
这样建边的话,最坏情况需要建立20W边。为了减少边数,我们可以在建边的时候,判断是否已有重边,若有只需合并权值即可(当然我认为在权值为INF无穷大的时候,就不用合并权值了)。
AC代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 2000
#define MAXM 500000
#define INF 100000000
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
}edge[MAXM];
int head[MAXN], edgenum;
int cur[MAXN];
int dist[MAXN];
bool vis[MAXN];
int Pig[MAXN];//每个猪圈里面的猪的数量
int want[MAXN];//每个顾客想买的猪的数量
vector<int> first[MAXN];//存储来到每个猪圈的顾客
int M, N;//M个猪圈 N个顾客
void init()
{
edgenum= 0;
memset(head, -1, sizeof(head));
for(int i = 1; i <= M; i++) first[i].clear();
}
void input()
{
int IT, a;
for(int i = 1; i <= M; i++) scanf("%d", &Pig[i]);
for(int i = 1; i <= N; i++)
{
scanf("%d", &IT);
while(IT--)
{
scanf("%d", &a);//可以打开a猪圈
first[a].push_back(i);//first[i][0]代表第一个来到i猪圈的顾客
}
scanf("%d", &want[i]);
}
}
void addEdge(int u, int v, int w)
{
int i;
//先判断是否有重的 合并
for(i = head[u]; i != -1; i = edge[i].next)
{
if(edge[i].to == v)//重复
break;
}
if(i == -1)//没有重边
{
Edge E1 = {u, v, w, 0, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
else//有重的 合并流量
{
if(w != INF) edge[i].cap += w;
}
}
void getMap()
{
//源点0连接所有到达猪圈的第一位顾客 权值为猪圈猪的数量
//对于紧接着来同一个猪圈的顾客建一条权值为无穷大的边
for(int i = 1; i <= M; i++)
{
int u = first[i][0];//每个猪圈第一位来的顾客
addEdge(0, u, Pig[i]);
//addEdge(u, 0, 0);
for(int j = 0; j < first[i].size() - 1; j++)
{
int x = first[i][j];
int y = first[i][j+1];
//printf("%d %d\n", x, y);
addEdge(x, y, INF);//接着来同一个猪圈的顾客 建边
}
}
//每个顾客连接汇点N+1 权值为他想买的猪的数量
for(int i = 1; i <= N; i++)
addEdge(i, N+1, want[i]);
}
bool BFS(int start, int end)//寻找是否存在增广路
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, false, sizeof(vis));
Q.push(start);
vis[start] = true;
dist[start] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow)//未标记 且没有满流
{
dist[E.to] = dist[u] + 1;//建立层次图
vis[E.to] = true;
if(E.to == end) return true;//找到增广路
Q.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int end)
{
if(x == end || a == 0) return a;//优化
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next)//从上次查找的弧开始
{
Edge &E = edge[i];
//选择最少残余量
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), end)) > 0)
{
E.flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int start, int end)
{
int flow = 0;
while(BFS(start, end))//查询是否存在增广路
{
memcpy(cur, head, sizeof(head));
flow += DFS(start, INF, end);
}
return flow;
}
int main()
{
while(scanf("%d%d", &M, &N) != EOF)
{
init();
input();
getMap();//建图
printf("%d\n", Maxflow(0, N+1));
}
return 0;
}