1 条题解
-
0
C++ :
#include<bits/stdc++.h> using namespace std; #define gc (c=getchar()) int read() { char c; while(gc<'0'); int x=c-'0'; while(gc>='0')x=x*10+c-'0'; return x; } #define rep(i,l,r) for(int i=l;i<=r;++i) namespace flow { const int N=1050,M=4*1000*2+5,oo=2139062143; int n,S,T; int t[N],a[N],g[N]; struct edge { int to,next,f; }l[M];int e=1; void add_e(int x,int y,int f=oo) { l[++e]=(edge){y,t[x],f};t[x]=e; l[++e]=(edge){x,t[y],0};t[y]=e; } int dfs(int x,int f) { if(x==T)return f; int f0=f; for(int &i=a[x],y;y=l[i].to;i=l[i].next) if(l[i].f&&g[y]<g[x]) { int del=dfs(y,min(f,l[i].f)); l[i].f-=del;l[i^1].f+=del; f-=del; if(!f) return f0; } return f0-f; } int q[N],head,tail; bool bfs() { memset(g+1,(1<<7)-1,n*sizeof(int)); g[T]=0; tail=1;q[1]=T; for(int head=1;head<=tail;++head) { int x=q[head]; for(int i=t[x],y;y=l[i].to;i=l[i].next) if(l[i^1].f&&g[y]==2139062143) { g[y]=g[x]+1; if(y==S)return 1; q[++tail]=y; } } return 0; } int max_flow() { int ans=0; while(bfs()) { memcpy(a+1,t+1,n*sizeof(int)); ans+=dfs(S,oo); } return ans; } void write(int x) { for(int i=t[x],y;y=l[i].to;i=l[i].next) if(l[i].f&&y!=T) printf("%d ",y); puts(""); } }; int main() { //freopen("1.in","r",stdin); int n,m; scanf("%d%d",&n,&m); flow::n=n*m+2;flow::S=flow::n-1;flow::T=flow::n; int now=0,sum=0; rep(i,1,n) rep(j,1,m) { ++now; int w=read();sum+=w; if(i+j&1) flow::add_e(now,flow::T,w); else {flow::add_e(flow::S,now,w); if(i!=1) flow::add_e(now,now-m); if(i!=n) flow::add_e(now,now+m); if(j!=1) flow::add_e(now,now-1); if(j!=m) flow::add_e(now,now+1); } } cout<<sum-flow::max_flow(); }
- 1
信息
- ID
- 995
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者