codevs 1907 解题报告 网络流
来源:互联网 发布:网络用语xp是什么意思 编辑:程序博客网 时间:2024/06/03 04:10
方格取数 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;}
好东西
阅读全文
2 0
- codevs 1907 解题报告 网络流
- CodeVS 1907 网络流最小割 解题报告
- codevs 3336 电话网络 SPFA 解题报告
- [CODEVS]数据结构系列 解题报告
- CODEVS 1022 覆盖 解题报告
- [codevs 1515]跳 【解题报告】
- Codevs 1251 括号 解题报告
- Codevs 1557 热浪 解题报告
- CodeVS 2370 LCA 解题报告
- codevs 1006 等差数列 解题报告
- 【codevs天梯&青铜Bronze】解题报告
- Codevs 1503 愚蠢的宠物 解题报告
- Codevs 1008 选数 解题报告
- 【解题报告】食物链[codevs 1074][rqnoj 455]
- 【解题报告】CodeVS 4600 程序自动分析
- Codevs 2292 图灵机游戏 解题报告
- Codevs 1026 逃跑的拉尔夫 解题报告
- CodeVS 1288 迭代加深 解题报告
- 计算机硬件入门 之 译码器(以74LS138为例)
- 新一代Java模板引擎Thymeleaf
- 统一访问方式和setter ----《Scala编程思想》
- 多线程2:线程操作共享数据所引发的问题
- POJ 1628 Deduction 笔记
- codevs 1907 解题报告 网络流
- (191)明暗处理
- USACO-Section1.2 Name That Number
- 如何给运行的tomcat指定特定的jdk版本以及验证
- 【剑指offer-解题系列(41)】和为S的连续正数序列
- 461. Hamming Distance
- jquery框架学习(keep update)
- 地图下载器破解版之太乐地图下载器4.9 破解教程(破解安装包见上传资源)
- Hat's Fibonacci(大数)