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;
    }
  • 相关阅读:
    如何阅读大型项目的代码?
    常用法律常识链接
    ubuntu 常用参数设置
    访问内存过程小结
    Netfilter深度解剖
    linux中的namespace
    style="display:none"隐藏html的标签
    xshell用ssh连接VMware中的ubuntu
    rails rake和示例
    rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each' for #...
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319505.html
Copyright © 2011-2022 走看看