【洛谷4313】 文理分科
作者:互联网
传送门
Solution
考虑和happiness一个套路,新建一个点然后连边就好了。
代码实现
/*
mail: mleautomaton@foxmail.com
author: MLEAutoMaton
This Code is made by MLEAutoMaton
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
int n,m,id[110][110],sum,s,t,wa[5]={0,1,-1,0,0},lk[5]={1,0,0,-1,0};
const int N=700010,Inf=1e9+10;
int front[N],cnt,cur[N];
struct node
{
int to,nxt,w;
}e[5000010];
queue<int>Q;
int dep[N];
void Add(int u,int v,int w)
{
e[cnt]=(node){v,front[u],w};front[u]=cnt++;
e[cnt]=(node){u,front[v],0};front[v]=cnt++;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
Q.push(s);dep[s]=1;
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=front[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(!dep[v] && e[i].w)
{
dep[v]=dep[u]+1;Q.push(v);
}
}
}
return dep[t];
}
int dfs(int u,int flow)
{
if(u==t || !flow)return flow;
for(int &i=cur[u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(dep[v]==dep[u]+1 && e[i].w)
{
int di=dfs(v,min(flow,e[i].w));
if(di)
{
e[i].w-=di;e[i^1].w+=di;
return di;
}
else dep[v]=0;
}
}
return 0;
}
int dinic()
{
int flow=0;
while(bfs())
{
for(int i=s;i<=t;i++)cur[i]=front[i];
while(int d=dfs(s,Inf))flow+=d;
}
return flow;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif
memset(front,-1,sizeof(front));
n=gi();m=gi();int tot=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)id[i][j]=++tot;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int x=gi();sum+=x;
Add(s,id[i][j],x);
}
t=tot*3+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int x=gi();sum+=x;
Add(id[i][j],t,x);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
++tot;
int x=gi();
Add(s,tot,x);sum+=x;
for(int k=0;k<5;k++)
{
int x=i+wa[k],y=j+lk[k];
if(x<1 || y<1 || x>n || y>m)continue;
Add(tot,id[x][y],Inf);
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
++tot;
int x=gi();sum+=x;
Add(tot,t,x);
for(int k=0;k<5;k++)
{
int x=i+wa[k],y=j+lk[k];
if(x<1 || y<1 || x>n || y>m)continue;
Add(id[x][y],tot,Inf);
}
}
printf("%d\n",sum-dinic());
return 0;
}
标签:dep,4313,洛谷,di,int,文理,front,return,include 来源: https://www.cnblogs.com/mle-world/p/10555706.html