codevs 1907 解题报告 网络流
2017-05-30 11:00
387 查看
方格取数 3
时间限制: 2 s空间限制: 256000 KB
题目等级 : 大师 Master
题目描述 Description
问题描述:
在一个有m*n 个方格的棋盘中,每个方格中有一个正整数。现要从方格中取数,使任意2 个数所在方格没有公共边,且取出的数的总和最大。试设计一个满足要求的取数算法。
编程任务:
对于给定的方格棋盘,按照取数要求编程找出总和最大的数。#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> using namespace std; const int NN=1000; const int inf=0x3f3f3f3f; int N,M,ans,head[NN],next[NN<<3],to[NN<<3],c[NN<<3],tot=1,S=0,T=999; int dist[NN],last[NN],num[NN],SUM; bool Exit; void add(int a,int b,int v) { to[++tot]=b; c[tot]=v; next[tot]=head[a]; head[a]=tot; last[a]=head[a]; } void input() { int x,t=0,color=0; scanf("%d%d",&N,&M); for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) { if (j==1) {color=(M&1)?!color:color;} else color=!color; scanf("%d",&x); SUM+=x; t++; if(color) { add(S,t,x),add(t,S,0); if (i>1) add(t,t-M,inf),add(t-M,t,0); if (i<N) add(t,t+M,inf),add(t+M,t,0); if (j>1) add(t,t-1,inf),add(t-1,t,0); if (j<M) add(t,t+1,inf),add(t+1,t,0); } else add(t,T,x),add(T,x,0); } } int dfs(int x,int in) { if (x==T) return in; int t,ans=0; for (int p=last[x];p;last[x]=p=next[p]) if(c[p]&&dist[x]==dist[to[p]]+1) { ans+= t=dfs(to[p],min(c[p],in-ans)); c[p]-=t;c[p^1]+=t; if(Exit||ans==in) return ans; } Exit=--num[dist[x]]==0;num[++dist[x]]++; last[x]=head[x]; return ans; } int main() { int flow=0; input(); num[0]=N*M+2; while(!Exit) flow+=dfs(S,inf); printf("%d\n",SUM-flow); return 0; }
好东西
相关文章推荐
- CodeVS 1907 网络流最小割 解题报告
- BZOJ 3993 [SDOI 2015] 网络流+二分答案 解题报告
- 网络流24题解题报告小结
- [CODEVS]数据结构系列 解题报告
- BZOJ 1001 网络流最小割 解题报告
- codevs 1073 家族 并查集 解题报告
- pku 1087 A Plug for UNIX 网络流 解题报告
- 【POJ 2396】 Budget 带上下界网络流 解题报告
- 【网络流之最大权闭包专题】解题报告
- Codeforces 808F 网络流最小割(二分图最大点权独立集) 解题报告
- codevs 1036 商务旅行 LCA 解题报告
- codevs 2594 解题报告 启发式搜索
- HDOJ 3605 Escape 解题报告 二分图匹配 dinic 网络流
- URAL 1277 网络流最小割 解题报告
- codevs 1464 装箱问题2 模拟 解题报告
- SPOJ IM 962 网络流最大流 解题报告
- POJ 1637 网络流判混合欧拉回路 解题报告
- [BZOJ1391]解题报告|网络流的又一类建图&Dinic的若干优化
- BZOJ 3894 网络流最小割 解题报告
- SPOJ 287 网络流最大流+二分答案 解题报告