# hdu 1565 方格取数(1) and hdu 1569 方格取数(2)

For solve this problem.I consult two blogs:

http://yzmduncan.iteye.com/blog/1149057

http://www.cnblogs.com/wally/archive/2013/05/05/3061465.html

Code 1565:

#include <cstdio>
#include <cstdlib>
#include <memory.h>
#include <cstring>

const int MAXN = 100010;
const int MAXE = 400010;
const int INF = 0x3f3f3f3f;

struct Edge{
int to,next,cap,flow;
}edge[MAXE];

int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init(){
tol = 0;
memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w,int rw = 0){
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = 0;head[u] = tol ++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = 0;head[v] = tol ++;
}

int sap(int start,int end,int N){
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N){
if(u == end){
int Min = INF;
for(int i= pre[u];i!=-1;i=pre[edge[i^1].to]){
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i= pre[u];i!=-1;i=pre[edge[i^1].to]){
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i=cur[u];i != -1;i = edge[i].next){
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]){
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag){
u = v;
continue;
}
int Min = N;
for(int i = head[u];i != -1;i=edge[i].next){
if(edge[i].cap-edge[i].flow && dep[edge[i].to] < Min){
Min = dep[edge[i].to];
cur[u] = i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]] ++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}

int mat[25][25];
int f[25][25];

void Deal_with(){
int n,total;
while(~scanf("%d",&n)){
init();
total = 0;
int sum_f = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",f[i]+j);
mat[i][j] = ++ total;
sum_f += f[i][j];
}
}
int start_point = 0,end_point = n * n + 1;
total += 2;
for(int i=0;i<n;i++){
for(int j =0;j<n;j++){
if((i+j) % 2){
addedge(mat[i][j],end_point,f[i][j]);
}
else {
addedge(start_point,mat[i][j],f[i][j]);
for(int x=-1;x<=1;x++){
for(int y =-1;y<=1;y++){
if(abs(x) + abs(y) != 1)continue;
if(i + x < 0|| i + x >= n || j + y < 0 || j + y >= n)continue;
addedge(mat[i][j],mat[i+x][j+y],INF);
}
}
}
}
}
int ans = sap(start_point,end_point,total);
printf("%d\n",sum_f - ans);
}
}

int main(void){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}


Code 1569:

#include <cstdio>
#include <cstdlib>
#include <memory.h>
#include <cstring>

const int MAXN = 100010;
const int MAXE = 400010;
const int INF = 0x3f3f3f3f;

struct Edge{
int to,next,cap,flow;
}edge[MAXE];

int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init(){
tol = 0;
memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w,int rw = 0){
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = 0;head[u] = tol ++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = 0;head[v] = tol ++;
}

int sap(int start,int end,int N){
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N){
if(u == end){
int Min = INF;
for(int i= pre[u];i!=-1;i=pre[edge[i^1].to]){
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i= pre[u];i!=-1;i=pre[edge[i^1].to]){
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i=cur[u];i != -1;i = edge[i].next){
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]){
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag){
u = v;
continue;
}
int Min = N;
for(int i = head[u];i != -1;i=edge[i].next){
if(edge[i].cap-edge[i].flow && dep[edge[i].to] < Min){
Min = dep[edge[i].to];
cur[u] = i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]] ++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}

int mat[55][55];
int f[55][55];

void Deal_with(){
int n,m,total;
while(~scanf("%d %d",&n,&m)){
init();
total = 0;
int sum_f = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",f[i]+j);
mat[i][j] = ++ total;
sum_f += f[i][j];
}
}
int start_point = 0,end_point = n * m + 1;
total += 2;
for(int i=0;i<n;i++){
for(int j =0;j<m;j++){
if((i+j) % 2){
addedge(mat[i][j],end_point,f[i][j]);
}
else {
addedge(start_point,mat[i][j],f[i][j]);
for(int x=-1;x<=1;x++){
for(int y =-1;y<=1;y++){
if(abs(x) + abs(y) != 1)continue;
if(i + x < 0|| i + x >= n || j + y < 0 || j + y >= m)continue;
addedge(mat[i][j],mat[i+x][j+y],INF);
}
}
}
}
}
int ans = sap(start_point,end_point,total);
printf("%d\n",sum_f - ans);
}
}

int main(void){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}


#### HDU1565 方格取数(1) 网络流

2015-07-25 14:32:48

#### HDU1565 方格取数(1) 题解

2017-04-14 09:11:48

#### hdu acm 1565 方格取数(1)

2016-03-06 18:01:53

#### HDU1565：方格取数(1) (状态压缩DP)

2014-04-22 23:34:10

#### hdu1565 方格取数(状态压缩)

2014-03-03 13:17:07

#### hdoj 1565 方格取数(1) 【状压DP 基础题目】

2015-07-30 20:53:16

#### HDU 1569 - 方格取数(2) 二分图最大点权独立集(构图最大流解)

2013-08-24 18:31:36

#### HDU-1565 方格取数(1) 网络流

2013-11-23 22:50:27

#### hdu 1569 方格取数(2)

2012-09-06 10:25:37

#### hdu(1569)方格取数(2)（dinic算法）

2013-08-16 10:25:55