zoukankan      html  css  js  c++  java
  • POJ3422 Kaka's Matrix Travels[费用流]

    Kaka's Matrix Travels
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9522   Accepted: 3875

    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 SUMhe 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.

    Sample Input

    3 2
    1 2 3
    0 2 1
    1 4 2
    

    Sample Output

    15

    Source

    POJ Monthly--2007.10.06, Huang, Jinsong

    方格取数加强版
    每个格子拆成两个点,连一条(1,-a[i][j])的边,在连一条(k-1,0)的边【注意我的t是格子(n,n),k-1是为了防止走了k+1多条边到(n,n)】
    格子向右和下连一条(k,0)的边
     
    最小费用然后取负(当然直接spfa跑最大也可以)
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    const int N=5005,M=2e4+5,INF=1e9;
    int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    int n,k,a[N][N],s,t,num;
    struct edge{
        int v,ne,c,f,w;
    }e[M<<1];
    int cnt,h[N];
    inline void ins(int u,int v,int c,int w){
        cnt++;
        e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].w=w;
        e[cnt].ne=h[u];h[u]=cnt;
        cnt++;
        e[cnt].v=u;e[cnt].c=0;e[cnt].f=0;e[cnt].w=-w;
        e[cnt].ne=h[v];h[v]=cnt;
    }
    inline int id(int i,int j){return (i-1)*n+j;}
    void build(){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                int t=id(i,j);
                ins(t,num+t,1,-a[i][j]);//printf("%d %d %d %d
    ",i,j,t,a[i][j]);
                ins(t,num+t,k-1,0);
                if(i!=n) ins(num+t,id(i+1,j),k,0);
                if(j!=n) ins(num+t,id(i,j+1),k,0);
            }
    }
    int d[N],q[N],head,tail,inq[N],pre[N],pos[N];
    inline void lop(int &x){if(x==N)x=1;}
    bool spfa(){
        memset(d,127,sizeof(d));
        memset(inq,0,sizeof(inq));
        head=tail=1;
        d[s]=0;inq[s]=1;q[tail++]=s;
        pre[t]=-1;
        while(head!=tail){
            int u=q[head++];inq[u]=0;lop(head);
            for(int i=h[u];i;i=e[i].ne){
                int v=e[i].v,w=e[i].w;
                if(d[v]>d[u]+w&&e[i].c>e[i].f){
                    d[v]=d[u]+w;
                    pre[v]=u;pos[v]=i;
                    if(!inq[v])q[tail++]=v,inq[v]=1,lop(tail); 
                }
            }
        }
        return pre[t]!=-1;
    }
    int mcmf(){
        int flow=0,cost=0;
        while(spfa()){
            int f=INF;
            for(int i=t;i!=s;i=pre[i]) f=min(f,e[pos[i]].c-e[pos[i]].f);
            flow+=f;cost+=d[t]*f;
            for(int i=t;i!=s;i=pre[i]){
                e[pos[i]].f+=f;
                e[((pos[i]-1)^1)+1].f-=f;
            }
        }//printf("mcmf %d %d
    ",flow,cost);
        return cost;
    }
    int main(int argc, const char * argv[]){
        n=read();k=read();num=n*n;
        s=1;t=num+num;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++) a[i][j]=read();
        build();
        printf("%d",-mcmf());
    }
     
     
  • 相关阅读:
    请求重定向,请求转发
    post、get方法乱码问题
    Servlet
    修改Servlet模板,让Servlet更清新
    Java-Python对垒之质数计算
    使用Packet Tracer对不同网段组网模拟
    哑编码的两种方法
    AdaBoost scikit-learn相关参数
    KNN scikit-learn相关参数
    递归思想的应用-根据二叉树的中序遍历和前序遍历重建二叉树
  • 原文地址:https://www.cnblogs.com/candy99/p/6127130.html
Copyright © 2011-2022 走看看