zoukankan      html  css  js  c++  java
  • poj_3422_Kaka's Matrix Travels

    Description

    On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.

    Input

    The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.

    Output

    The maximum SUM Kaka can obtain after his Kth travel.

    题解

    建图:拆点,把一个点拆成两个点,每两个点连一条边。 设置一个源点和汇点,上模版。

    代码:

    #include <iostream>
    #include <queue>
    #include <string.h>
    using namespace std;
    #define inf  1 << 30
    #define M 5005
    int n, m, nm, ans;
    int map[51][51],head[M],qu[M],dis[M];
    bool v[M];
    
    struct node {
        int x,y,v,w,next;
    } e[100005];
    void add(int x, int y, int w, int f){
        e[nm].x=x; e[nm].y=y; e[nm].v=w; e[nm].w=f;
        e[nm].next=head[x];
        head[x]=nm;
        nm++;
        e[nm].x=y; e[nm].y=x; e[nm].v=-w; e[nm].w=0;
        e[nm].next=head[y];
        head[y]=nm;
        nm++;
    }
    
    bool spfa() {
        for (int i=0;i<=n*n*2+1;i++){
            qu[i] = -1; dis[i] = inf;  
            v[i] = false;  
        }  
        queue<int>q;
        dis[n*n*2]=0; v[n*n*2]=true;
        q.push(n*n*2);
        int i;
        while (!q.empty()){
            int t=q.front();
            q.pop();
            i=head[t]; v[t]=false;
            while (i!=-1){
                if (e[i].w>0&&dis[e[i].y]>dis[t]+e[i].v){
                    dis[e[i].y]=dis[t]+e[i].v;
                    qu[e[i].y]=i;
                    if (!v[e[i].y]){
                        v[e[i].y]=true;
                        q.push(e[i].y);
                    }
                }
                i=e[i].next;
            }
        }
        if (qu[n*n*2+1]==-1) return false;
        return true;
    }
    void getflow() {
        while (spfa()) {
            int max1=inf;
            int p=qu[n*n*2+1];
            while (p!=-1) {
                max1=min(max1,e[p].w);
                p=qu[e[p].x];
            }
            p=qu[n*n*2+1];
            while (p!=-1){
                e[p].w-=max1;
                e[p^1].w+=max1;
                ans+=max1*e[p].v;
                p=qu[e[p].x];
            }
        }
    }
    
    int main() {
        while (cin>>n>>m) {
            int i,j;
            nm=0;
            for (i=1;i<=n;i++)
                for (j=1;j<=n;j++)
                    cin>>map[i][j];
            memset(head,-1,sizeof(head));
            for (i=1;i<=n;i++)
                for (j=1;j<=n;j++){
                    int x=(i-1)*n+j-1;
                    add(x*2,x*2+1,-map[i][j],1);
                    add(x*2,x*2+1,0,m-1);
                }
            for (i=1;i<=n;i++)
                for (j=1;j<n;j++){
                    int x=(i-1)*n+j-1;
                    add(x*2+1,(x+1)*2,0,m);
                }
            for (i=1;i<n;i++)  
                for (j=1;j<=n;j++){
                    int x=(i-1)*n+j-1;  
                    add(x*2+1,(x+n)*2,0,m);  
                }
            add(n*n*2,0,0,m);
            add(n*n*2-1,n*n*2+1,0,m);
            ans=0;
            getflow();
            cout<<-ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Linux命令:ls
    Log4j:log4j.properties 配置解析
    PostgreSQL: WITH Queries (Common Table Expressions)
    Linux使用SecureCRT上传下载
    PostgreSQL 自动输入密码
    Linux命令:rmdir
    Linux命令:mkdir
    Linux命令:pwd
    Linux命令:cd
    正则表达式 
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319505.html
Copyright © 2011-2022 走看看