标签:个人 技术分享 nic blog sizeof std 例子 rom com
//It is coded by ning-mew on 6.28
#include<bits/stdc++.h>
#define FOR for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
using namespace std;
const int maxn=507,INF=1e9+7;
int n,m,S=0,T=maxn*maxn*6-1,ANS=0;
int head[maxn*maxn*6],cnt=-1,last[maxn*maxn*6];
struct Edge{int nxt,to,dis;}edge[maxn*maxn*25];
int art[maxn][maxn],sce[maxn][maxn];
int same_art[maxn][maxn],same_sce[maxn][maxn];
int add_x[5]={0,0,0,1,-1},add_y[5]={0,1,-1,0,0};
int Node(int x,int y,int num){
return n*m*(num-1)+(x-1)*m+y;
}
void add(int from,int to,int dis){
edge[++cnt].nxt=head[from];edge[cnt].to=to;
edge[cnt].dis=dis;head[from]=cnt;
}
void Add(int from,int to,int dis){
//cout<<from<<' '<<to<<' '<<dis<<endl;
add(from,to,dis);add(to,from,0);return;
}
struct Network{
int depth[maxn*maxn*6],mark[maxn*maxn*6];
void clear(){
memset(depth,0,sizeof(depth));
memset(mark,0,sizeof(mark));
}
bool bfs(int x){
queue<int>Q;while(!Q.empty())Q.pop();
Q.push(S);depth[S]=1;mark[S]=x;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].to;
if(mark[v]!=x&&edge[i].dis>0){
mark[v]=x;depth[v]=depth[u]+1;
Q.push(v);
}
}
}if(mark[T]==x)return true;return false;
}
int dfs(int u,int dist){
if(u==T)return dist;int d=0;
for(int &i=last[u];i!=-1;i=edge[i].nxt){
int v=edge[i].to;
if(mark[v]==mark[u]&&depth[v]==depth[u]+1&&edge[i].dis>0){
d=dfs(v,min(edge[i].dis,dist));
if(d){
edge[i].dis-=d;edge[i^1].dis+=d;
return d;
}
}
}return 0;
}
int Dinic(){ clear();
int ans=0,d=0,x=2;
while(bfs(++x)){
d=dfs(S,INF);
while(d){/*cout<<"Dinic:"<<d<<endl;*/ans+=d;d=dfs(S,INF);}
for(int i=0;i<=m*n*4;i++)last[i]=head[i];last[T]=head[T];
}return ans;
}
}Net;
int main(){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
FOR scanf("%d",&art[i][j]),ANS+=art[i][j],
Add(S,Node(i,j,1),art[i][j]),Add(Node(i,j,1),Node(i,j,2),INF);
FOR scanf("%d",&sce[i][j]),ANS+=sce[i][j],
Add(Node(i,j,2),T,sce[i][j]);
FOR {
scanf("%d",&same_art[i][j]);ANS+=same_art[i][j];
Add(S,Node(i,j,3),same_art[i][j]);
for(int k=0;k<=4;k++){
int ii=i+add_x[k],jj=j+add_y[k];
if(ii>0&&ii<=n&&jj>0&&jj<=m){
Add(Node(i,j,3),Node(ii,jj,1),INF);
}
}
}
FOR {
scanf("%d",&same_sce[i][j]);ANS+=same_sce[i][j];
Add(Node(i,j,4),T,same_sce[i][j]);
for(int k=0;k<=4;k++){
int ii=i+add_x[k],jj=j+add_y[k];
if(ii>0&&ii<=n&&jj>0&&jj<=m){
Add(Node(ii,jj,2),Node(i,j,4),INF);
}
}
}
printf("%d\n",ANS-Net.Dinic());
return 0;
}
博主蒟蒻,随意转载。但必须附上原文链接:http://www.cnblogs.com/Ning-Mew/,否则你会终生找不到妹子!!!
标签:个人 技术分享 nic blog sizeof std 例子 rom com
原文地址:https://www.cnblogs.com/Ning-Mew/p/9240333.html