BZOJ1324: Exca王者之剑

时间:2021-04-12 21:31:29

1324: Exca王者之剑

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 256  Solved: 131
[Submit][Status]

Description

BZOJ1324: Exca王者之剑
BZOJ1324: Exca王者之剑

Input

第一行给出数字N,M代表行列数.N,M均小于等于100
下面N行M列用于描述数字矩阵

Output

输出最多可以拿到多少块宝石

Sample Input

2 2
1 2
2 1

Sample Output

4

HINT

Source

题解:
其实就是相邻格子不能取,然后最小割。。。
机房好冷啊
代码:
 #include<cstdio>

 #include<cstdlib>

 #include<cmath>

 #include<cstring>

 #include<algorithm>

 #include<iostream>

 #include<vector>

 #include<map>

 #include<set>

 #include<queue>

 #include<string>

 #define inf 1000000000

 #define maxn 10000+5

 #define maxm 100000+5

 #define eps 1e-10

 #define ll long long

 #define pa pair<int,int>

 #define for0(i,n) for(int i=0;i<=(n);i++)

 #define for1(i,n) for(int i=1;i<=(n);i++)

 #define for2(i,x,y) for(int i=(x);i<=(y);i++)

 #define for3(i,x,y) for(int i=(x);i>=(y);i--)

 #define mod 1000000007

 using namespace std;

 inline int read()

 {

     int x=,f=;char ch=getchar();

     while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}

     while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}

     return x*f;

 }
int n,m,s,t,ans,tot=,head[maxn],cur[maxn],h[maxn],q[maxn],num[][]; struct edge{int go,next,v;}e[maxm];
const int dx[]={,,-,};
const int dy[]={,,,-}; void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;} void insert(int x,int y,int z){ins(x,y,z);ins(y,x,);} bool bfs() { for(int i=s;i<=t;i++)h[i]=-; int l=,r=;q[]=s;h[s]=; while(l<r) { int x=q[++l]; for(int i=head[x];i;i=e[i].next) if(e[i].v&&h[e[i].go]==-) { h[e[i].go]=h[x]+;q[++r]=e[i].go; } } return h[t]!=-; } int dfs(int x,int f) { if(x==t) return f; int tmp,used=; for(int i=cur[x];i;i=e[i].next) if(e[i].v&&h[e[i].go]==h[x]+) { tmp=dfs(e[i].go,min(e[i].v,f-used)); e[i].v-=tmp;if(e[i].v)cur[x]=i; e[i^].v+=tmp;used+=tmp; if(used==f)return f; } if(!used) h[x]=-; return used; } void dinic() { while(bfs()) { for (int i=s;i<=t;i++)cur[i]=head[i];ans-=dfs(s,inf); } } int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();m=read();s=;t=n*m+;
for1(i,n)for1(j,m)
{
int x=read();
num[i][j]=(i-)*m+j;ans+=x;
if((i+j)&)insert(s,num[i][j],x);else insert(num[i][j],t,x);
}
for1(i,n)for1(j,m)if((i+j)&)
for0(k,)
{
int x=i+dx[k],y=j+dy[k];
if(x<||x>n||y<||y>m)continue;
insert(num[i][j],num[x][y],inf);
}
dinic();
printf("%d\n",ans); return ; }