标签:new turn using include reg ems int dfs emc
和上一题差不多,每行和每列分别看做一个点,障碍点坐标的行和列就不建边,再按照有源汇上下界建图就好了,唯一的区别就是这个题求的是最小流
这个题的数据好水呢,建错图也能A呢
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
#define rg register
#define min(a,b) (a<b?a:b)
int dis[2001],ans,n,m,k,cnt=1,s,t,ss,tt,sum,inf=1e9,pre[31000],nxt[31000],h[2001],v[30001],cur[2001],in[2001];
queue<int>q;bool mp[101][101];
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
void add(int x,int y,int z)
{
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,v[cnt]=z;
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt;
}
bool bfs(int s,int t)
{
memset(dis,0,sizeof dis);
q.push(s),dis[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=h[x];i;i=nxt[i])if(!dis[pre[i]]&&v[i])dis[pre[i]]=dis[x]+1,q.push(pre[i]);
}
return dis[t];
}
int dfs(int x,int flow,int t)
{
if(x==t||!flow)return flow;
int f=flow;
for(int &i=cur[x];i;i=nxt[i])
if(v[i]&&dis[pre[i]]>dis[x])
{
int y=dfs(pre[i],min(v[i],f),t);
f-=y,v[i]-=y,v[i^1]+=y;
if(!f)return flow;
}
if(f==flow)dis[x]=-1;
return flow-f;
}
int main()
{
read(n),read(m),read(k);s=0,t=n+m+1,ss=t+1,tt=ss+1;
for(rg int i=1,x;i<=n;i++)read(x),add(s,i,inf-x),in[i]+=x,in[s]-=x;
for(rg int i=1,x;i<=m;i++)read(x),add(i+n,t,inf-x),in[t]+=x,in[i+n]-=x;
for(rg int i=1,x,y;i<=k;i++)read(x),read(y),mp[x][y]=1;
for(rg int i=1;i<=n;i++)
for(rg int j=1;j<=m;j++)
if(!mp[i][j])add(i,j+n,1);
for(int i=0;i<=t;i++)
{
if(in[i]>0)add(ss,i,in[i]),sum+=in[i];
if(in[i]<0)add(i,tt,-in[i]);
}
add(t,s,inf);
for(;bfs(ss,tt);ans+=dfs(ss,inf,tt))memcpy(cur,h,sizeof cur);
if(ans!=sum){printf("JIONG\n");return 0;}
sum=0,ans=v[cnt];
for(int i=h[ss];i;i=nxt[i])v[i]=v[i^1]=0;
for(int i=h[tt];i;i=nxt[i])v[i]=v[i^1]=0;
v[cnt]=v[cnt^1]=0;
for(;bfs(t,s);sum+=dfs(t,inf,s))memcpy(cur,h,sizeof cur);
printf("%d\n",ans-sum);
}
标签:new turn using include reg ems int dfs emc
原文地址:https://www.cnblogs.com/lcxer/p/10224310.html