【wikioi】1907 方格取数3(最大流+最大权闭合子图)
http://www.wikioi.com/problem/1907/
这题我一开始想到的是状压,看到n<=30果断放弃。
然后也想到了黑白染色,然后脑残了,没想到怎么连边。
很简单的一题
黑白染色后,黑格子或白格子向四周连边(两种格子同时连会错,因为这样就都是环了,每个点都可以取或不取),容量为oo,然后如果是黑(白)节点,源连一条边,容量为权值;如果是白(黑)节点,连一条边到汇,容量为权值。
最后答案为所有格子权值和-最大流(其实是最小割)
ps:(其实就是之前做过的qq农场 囧。。http://www.cnblogs.com/iwtwiioi/p/3893519.html
为什么这样做参考我以前写的博文(http://www.cnblogs.com/iwtwiioi/p/3872099.html)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1000, M=N<<4, oo=~0u>>1;
struct ED { int u, v, next, cap; } e[M];
int n, m, cnt=1;
int d[N], gap[N], cur[N], p[N], ihead[N];
inline int id(const int &x, const int &y) { return (x-1)*m+y; }
inline void add(const int &u, const int &v, const int &c) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].v=v; e[cnt].u=u; e[cnt].cap=c;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].v=u; e[cnt].u=v; e[cnt].cap=0;
}
int isap(const int &s, const int &t, const int &n) {
int ret=0, f, u, v, i;
for1(i, 0, n) cur[i]=ihead[i];
gap[0]=n; u=s;
while(d[s]<n) {
for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]==d[e[i].v]+1) break;
if(i) {
v=e[i].v; p[v]=cur[u]=i; u=v;
if(u==t) {
for(f=oo; u!=s; u=e[p[u]].u) f=min(f, e[p[u]].cap);
for(u=t; u!=s; u=e[p[u]].u) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
ret+=f;
}
}
else {
if(! (--gap[d[u]]) ) break;
d[u]=n;
cur[u]=ihead[u];
for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].v]+1) d[u]=d[e[i].v]+1;
++gap[d[u]];
if(u!=s) u=e[p[u]].u;
}
}
return ret;
}
int main() {
read(n); read(m);
int ans=0, t, now, S=id(n, m)+1, T=id(n, m)+2;
for1(i, 1, n) for1(j, 1, m) {
read(t); ans+=t;
now=id(i, j);
if((i+j)%2) {
add(S, now, t);
if(j<m) add(now, id(i, j+1), oo);
if(i<n) add(now, id(i+1, j), oo);
if(j>1) add(now, id(i, j-1), oo);
if(i>1) add(now, id(i-1, j), oo);
}
else add(now, T, t);
}
print(ans-isap(S, T, T));
return 0;
}
题目描述 Description
«问题描述:
在一个有m*n 个方格的棋盘中,每个方格中有一个正整数。现要从方格中取数,使任
意2 个数所在方格没有公共边,且取出的数的总和最大。试设计一个满足要求的取数算法。
«编程任务:
对于给定的方格棋盘,按照取数要求编程找出总和最大的数。
输入描述 Input Description
第1 行有2 个正整数m和n,分别表示棋盘的行数
和列数。接下来的m行,每行有n个正整数,表示棋盘方格中的数。
输出描述 Output Description
将取数的最大总和输出
样例输入 Sample Input
3 3
1 2 3
3 2 3
2 3 1
样例输出 Sample Output
11
数据范围及提示 Data Size & Hint
n,m<=30
【wikioi】1907 方格取数3(最大流+最大权闭合子图)的更多相关文章
- Codevs 1227 方格取数 2(费用流)
1227 方格取数 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 查看运行结果 题目描述 Description 给出一个n*n的矩阵,每一格有一个非负整数 ...
- codevs 1907 方格取数 3
Description 在一个有m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意2 个数所在方格没有公共边,且取出的数的总和最大.试设计一个满足要求的取数算法. Input 第 ...
- 洛谷 - P2045 - 方格取数加强版 - 费用流
原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...
- 洛谷P2045 方格取数加强版(费用流)
题意 题目链接 Sol 这题能想到费用流就不难做了 从S向(1, 1)连费用为0,流量为K的边 从(n, n)向T连费用为0,流量为K的边 对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次, ...
- LG2045 方格取数加强版 费用流
问题描述 LG2045 题解 费用流. 套路拆点,把\((i,j)\)拆为两个点,在这两个点之间连边:一条边流量为\(1\),费用为\(a_{i,j}\),另一条边为流量为\(INF\),费用为\(0 ...
- 洛谷 - P2805 - 植物大战僵尸 - 最大流 - 最大权闭合子图
https://www.luogu.org/problemnew/show/P2805 最大权闭合子图的特点是,假如你要选一个结点,则要先选中它的所有子节点.正权连S负权连T,容量为绝对值,原图有向边 ...
- HDU 1565&1569 方格取数系列(状压DP或者最大流)
方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- LibreOJ #6007. 「网络流 24 题」方格取数 最小割 最大点权独立集 最大流
#6007. 「网络流 24 题」方格取数 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流)
Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流) Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从 ...
随机推荐
- Linux 守护进程和超级守护进程(xinetd)
一 .Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的 ...
- django template中load的作用
某些应用提供自定义标签和过滤器库. 要在一个模板中访问它们, 使用 {% load %} 标签: {% load comments %} {% comment_form for blogs.entri ...
- linux下查看文件夹的大小
du -sh du -sh dir_name/ du -sm * | sort -n //统计当前目录大小 并安大小排序 转自:http://www.jb51.net/LINUXjishu/77450 ...
- Rehashing
The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...
- malloc/free vs new/delete
malloc/new是库函数. new/delete是运算符. 对于非内部数据类型的对象而言,光用malloc/free 无法满足动态对象的要求.对象在创建的同时要自动执行构造函数, 对象在消亡之前要 ...
- iOS 定义圆角控件
ios7 以前,想把UILabel变为圆角的,只需要设置layer的 cornerRadius属性,ios7以后,还需要设置 masksToBounds = true. 以下是这个属性的说明 A Bo ...
- 通过百度echarts实现数据图表展示功能
现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...
- javascript类的类比详解-大白话版
转载请注明出处:水车 如果有误,还望指出,谢谢 -----------------正文分割线---------------------- 类:类太抽象,要想弄明白就该用现实的东西来类比 在我看来类就是 ...
- discuz 学习
一.Discuz首页“今日”“昨日”“欢迎新会员”等文字删除添加 搜索templeta/default/forum/discuz.htm (使用非默认模版的请修改当前使用模版的discuz.htm ...
- MapReduce:详解Shuffle过程(转)
/** * author : 冶秀刚 * mail : dennyy99@gmail.com */ Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapRedu ...