题目链接:
[POJ 1149]PIGS[最大流][建图]
题意分析:
有M个猪圈,每个猪圈有一定数量的猪,但是米尔科没有打开猪圈的钥匙,钥匙在顾客的手上。总共N个顾客,每个顾客拥有某些猪圈的钥匙,每次到来一个顾客,他都可以从他可以打开的猪圈选猪,需要选need只。每次选完后,米尔科可以选择把这些猪分配到任意一个打开的猪圈里面,然后关上猪圈。现在问:怎么安排可以使得猪被卖的最多,最多多少只?
解题思路:
每个顾客在选完这些猪后,他所打开的猪圈其实就变成了一个猪圈,所以可以将猪圈的猪个数相加,作为源点和顾客间连线的权值。那么如果下一个顾客需要用到没有被打开的猪圈和已经被打开过的猪圈呢?首先没有打开的猪圈按上面的样子合并起来,猪总数量作为源点和顾客的连线,已经被合并的猪圈,只需找到被谁合并,将这个谁与当前顾客连INF的权值即可。
更详细的思路请看:[网络流建模汇总][Edelweiss].pdf
很赞的一本书啊。
个人感受:
最初的想法在猪圈间连线,不可避免得出现了不必要的流。连最书中提到最简单的建模想法都没想到。。。。。
具体代码如下:
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#define lowbit(x) (x & (-x))
#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define ll long long
#define pr(x) cout << #x << " = " << (x) << '\n';
using namespace std;
const int MAXN = 200;//点数的最大值
const int MAXM = 200000;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge{
int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void addedge(int u,int v,int w,int rw = 0)
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[end] = 0;
Q[rear++] = end;
while(front != rear)
{
int u = Q[front++];
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dep[v] != -1)continue;
Q[rear++] = v;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int S[MAXN];
int sap(int start,int end,int N)
{
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top = 0;
int u = start;
int ans = 0;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
int inser;
for(int i = 0;i < top;i++)
if(Min > edge[S[i]].cap - edge[S[i]].flow)
{
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for(int i = 0;i < top;i++)
{
edge[S[i]].flow += Min;
edge[S[i]^1].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top]^1].to;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
{
flag = true;
cur[u] = i;
break;
}
}
if(flag)
{
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if(u != start)u = edge[S[--top]^1].to;
}
return ans;
}
void init() {
tol = 0;
memset(head,-1,sizeof(head));
}
int pig[1011], par[1011]; // par:该猪圈被谁连走了
int main()
{
int m, n, x, house;
scanf("%d%d", &m, &n);
init();
int src = 0, des = n + 1;
for (int i = 1; i <= m; ++i) {
par[i] = 0;
scanf("%d", &pig[i]);
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
int sum = 0, need;
for (int j = 0; j < x; ++j) {
scanf("%d", &house);
if (par[house]) {
addedge(par[house], i, INF);
}
else sum += pig[house], par[house] = i;
}
if (sum) addedge(src, i, sum);
scanf("%d", &need);
addedge(i, des, need);
}
printf("%d\n", sap(src, des, des + 1));
return 0;
}