关闭

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

标签: acmhduNetwork FlowSAP
187人阅读 评论(0) 收藏 举报

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


The portal:http://acm.hdu.edu.cn/showproblem.php?pid=1565

                             http://acm.hdu.edu.cn/showproblem.php?pid=1569

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;
}

                           

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59799次
    • 积分:3598
    • 等级:
    • 排名:第9135名
    • 原创:315篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论