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

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.

Sample Input

3 2
1 2 3
0 2 1
1 4 2

Sample Output


Source

POJ Monthly--2007.10.06, Huang, Jinsong
题意:
  1. 有个方阵,每个格子里都有一个非负数,从左上角走到右下角,每次走一步,只能往右或往下走,经过的数字拿走
  2. 每次都找可以拿到数字和最大的路径走,走k次,求最大和
  3. #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <algorithm>
    #include <set>
    using namespace std;
    #define MM(a,b) memset(a,b,sizeof(a))
    typedef long long ll;
    typedef unsigned long long ULL;
    const int mod = 1000000007;
    const double eps = 1e-10;
    const int inf = 0x3f3f3f3f;
    const int big=50000;
    int max(int a,int b) {return a>b?a:b;};
    int min(int a,int b) {return a<b?a:b;};
    const int N = 70;
    const int M=10000+100;
    struct edge{
    int to,cap,cost,rev;
    }; vector<edge> G[5005];
    int mp[55][55], dist[5005],inq[5005],prev[5005],prel[5005];
    struct Point{
    int x,y;
    }; int n,k,m,x,y,c,cnth,cntm; void add_edge(int u,int v,int cap,int cost)
    {
    G[u].push_back(edge{v,cap,cost,G[v].size()});
    G[v].push_back(edge{u,0,-cost,G[u].size()-1});
    } int mincost(int s,int t,int f)
    {
    int ans=0;
    while(f>0)
    {
    memset(dist,inf,sizeof(dist));
    memset(inq,0,sizeof(inq));
    dist[s]=0;
    queue<int> q;
    q.push(s);
    inq[s]=1;
    MM(prev,-1);
    while(!q.empty())
    {
    int u=q.front();
    q.pop();inq[u]=0;
    for(int j=0;j<G[u].size();j++)
    {
    edge &e=G[u][j];
    if(e.cap>0&&dist[e.to]>dist[u]+e.cost)
    {
    dist[e.to]=dist[u]+e.cost;
    prev[e.to]=u;
    prel[e.to]=j;
    if(!inq[e.to])
    {
    q.push(e.to);
    inq[e.to]=1;
    }
    }
    }
    }
    for(int i=t;i>s;)
    {
    int f=prev[i];
    if(f==-1) return -1;
    int j=prel[i];
    G[f][j].cap-=1;
    G[i][G[f][j].rev].cap+=1;
    ans+=G[f][j].cost;
    i=prev[i];
    }
    f-=1;
    }
    return ans;
    } int in(int i,int j)
    {
    return (i-1)*2*n+j;
    } int out(int i,int j)
    {
    return (i-1)*2*n+n+j;
    } void build(int k)
    {
    add_edge(0,1,k,0); for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {
    add_edge(in(i,j),out(i,j),1,-mp[i][j]);
    add_edge(in(i,j),out(i,j),k-1,0);
    } for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {
    if(i!=n)
    add_edge(out(i,j),in(i+1,j),k,0);
    if(j!=n)
    add_edge(out(i,j),in(i,j+1),k,0);
    } add_edge(out(n,n),out(n,n)+1,k,0);
    } int main()
    {
    while(~scanf("%d %d",&n,&k))
    {
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    scanf("%d",&mp[i][j]);
    build(k);
    printf("%d\n",-mincost(0,out(n,n)+1,k));
    }
    return 0;
    }

      分析:1.建图时一个节点拆成两个,并且中间连接两条边,一条是费用该点的价值取反,另一条是为了保证这个节点能经过多次

  4. 2因为是最小费用流,所以需要将费用取反,跑完最小费用流后再取反就好

POJ 3422 矩阵取数 最小费用流拆点+负边的更多相关文章

  1. 51Nod 1084 矩阵取数问题 V2 —— 最小费用最大流 or 多线程DP

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1084 1084 矩阵取数问题 V2  基准时间限制:2 秒 空 ...

  2. NOIP2007 矩阵取数游戏

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  3. NOIP2007矩阵取数[DP|高精度]

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  4. TYVJ 矩阵取数 Label:高精度+dp

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  5. 【NOIP2007】矩阵取数

    因为傻逼写错高精度搞了一下午浪费好多时间,好想哭qaq 原题: 帅帅经常更同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij据为非负整数.游戏规则如下: 1. 每次取数时须从每 ...

  6. 51nod1084 矩阵取数问题 V2

    O(n4)->O(n3)妈呀为什么跑这么慢woc #include<cstdio> #include<cstring> #include<cctype> #i ...

  7. 1166 矩阵取数游戏[区间dp+高精度]

    1166 矩阵取数游戏 2007年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description [ ...

  8. 矩阵取数游戏 NOIP 2007

    2016-05-31 17:26:45 题目链接: NOIP 2007 矩阵取数游戏(Codevs) 题目大意: 给定一个矩阵,每次在每一行的行首或者行尾取一个数乘上2^次数,求取完最多获得的分数 解 ...

  9. 洛谷 P1005 矩阵取数游戏

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

随机推荐

  1. 【Bell-Ford 算法】CLRS Exercise 24.1-4,24.1-6

    本文是一篇笔记,大部分内容取自 CLRS 第三版,第 24.1 节. Exercise 24.1-4 Modify the Bellman-Ford algorithm so that it sets ...

  2. 初入JavaWeb(半成品)

    2019-10-21 20:51:03 初次进行Javaweb的开发. 要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示 ...

  3. P2670 【扫雷游戏】

    题面哦~~ lalala~~~ 这题数据并不大,最大不过100*100,所以果断穷举 其实本来我是想边读边做的,但读入是个问题. 主要思路呢,就是读完之后穷举一边,只要是炸弹,就把周围一圈8个加一遍 ...

  4. CSUST 2012 一个顶俩 (本校OJ题)(思维+树链剖分)

    (点击这里查看原题,不保证可以进去....外网可能比较卡) Description A:一心一意 B:一个顶俩 最近QQ更新后那个成语接龙好像挺火的?但我只知道图论里一条边是一个顶俩个点的emm. 如 ...

  5. DataX操作指南

    1.DataX介绍 DataX DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hive.ADS.HB ...

  6. angular 4+中关于父子组件传值的示例

    home.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-hom ...

  7. SQL这样干,你就是给自己刨坑.....

    SQL是作为一个程序员接触得非常多的一种语言,但是,很多时候,我们会发现,有些SQL的执行效率异常的差,造成了数据库的负担.我们通过分析这些有问题的SQL,就可以发现很多我们平时在写SQL的时候忽略的 ...

  8. 移动端H5开发自适应技巧

    移动端H5开发,必要要做到自适应各种分辨率的手机,下面由我为大家大致说一下,需要3步走 第一:head标签中添加: <meta name="viewport" content ...

  9. 解决stackoverflow加载慢的插件

    浏览stackoverflow的时候,比较慢,网上找到一个大神写的小工具 挺管用,给推荐下. gitthub地址: https://github.com/justjavac/ReplaceGoogle ...

  10. vue-transition实现加入购物车效果及其他动画效果实现

    vue提供了<transition></transition>和<transition-group></transition-group>实现元素状态的 ...