# 【BZOJ2879】【NOI2012】美食节（费用流）

## 题解

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 88888
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Line{int v,next,w,fy;}e[MAX<<2];
int h[MAX],cnt=2;
inline void Add(int u,int v,int w,int fy)
{
e[cnt]=(Line){v,h[u],w,fy};h[u]=cnt++;
e[cnt]=(Line){u,h[v],0,-fy};h[v]=cnt++;
}
queue<int> Q;
int pv[MAX],pe[MAX],S,T,Flow,Cost,dis[MAX];
bool inq[MAX];
bool SPFA()
{
memset(dis,63,sizeof(dis));dis[S]=0;Q.push(S);
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v;
if(e[i].w&&dis[v]>dis[u]+e[i].fy)
{
dis[v]=dis[u]+e[i].fy;pv[v]=u;pe[v]=i;
if(!inq[v])inq[v]=true,Q.push(v);
}
}
inq[u]=false;
}
if(dis[T]>=1e9)return false;
int flow=1e9;
for(int i=T;i!=S;i=pv[i])flow=min(flow,e[pe[i]].w);
for(int i=T;i!=S;i=pv[i])e[pe[i]].w-=flow,e[pe[i]^1].w+=flow;
Cost+=flow*dis[T];
return true;
}
int n,m,t[111][111],a[111],tot;
int bl[MAX];
int main()
{
S=0;T=88880;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
for(int i=1;i<=m;++i)
{