POJ3422 Kaka's Matrix Travels

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

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

15

Source

POJ Monthly--2007.10.06, Huang, Jinsong

—————————————————————————————————

题意:N*N的地图上每格都有分数,分数只能获取一次。有人从左上方开始,每次向右或下移动一格,到右下方为止,记为一次环游。问第K次环游后累计分数的最大值?

思路:最小费用最大流。拆点,将每个点拆成两个,这两点之间连两条边,一条容量为1,费用为该节点的值,再连一条容量为无穷的边,费用为0。每个点只能往右或者往下走,所以将它和右边及下边的点连一条边,容量为无穷,费用为它的值。源点向第一个点连边,容量为k,费用为0,最后一个点向汇点连边,容量为k,费用为0。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 100100
#define MAXM 1000100

int vis[MAXN],d[MAXN],pre[MAXN],aa[MAXN];

struct Edge
{
    int u, v, c, cost, next;
} edge[MAXM];
int s[MAXN], cnt;

void init()
{
    cnt = 0;
    memset(s, -1, sizeof(s));
}

void add(int u, int v, int c, int cost)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cost = cost;
    edge[cnt].c = c;
    edge[cnt].next = s[u];
    s[u] = cnt++;
    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cost = -cost;
    edge[cnt].c = 0;
    edge[cnt].next = s[v];
    s[v] = cnt++;
}

bool spfa(int ss, int ee,int &flow,int &cost)
{
    queue<int> q;
    memset(d, INF, sizeof d);
    memset(vis, 0, sizeof vis);
    d[ss] = 0, vis[ss] = 1, pre[ss] = 0, aa[ss] = INF;
    q.push(ss);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = s[u]; ~i; i = edge[i].next)
        {
            int v = edge[i].v;
            if (edge[i].c>0&& d[v]>d[u] + edge[i].cost)
            {
                d[v] = d[u] + edge[i].cost;
                pre[v] = i;
               aa[v] = min(aa[u], edge[i].c);
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    if (d[ee] == INF) return 0;
    flow += aa[ee];
    cost += d[ee]*aa[ee];
    int u = ee;
    while (u != ss)
    {
        edge[pre[u]].c -= aa[ee];
        edge[pre[u] ^ 1].c += aa[ee];
        u = edge[pre[u]].u;
    }
    return 1;
}

int MCMF(int ss, int ee)
{
    int cost = 0, flow=0;
    while (spfa(ss, ee, flow, cost));

    return cost;
}



int main()
{
  int n,k;
  int a[5000];
  while(~scanf("%d%d",&n,&k))
  {
      init();
      for(int i=1;i<=n*n;i++)
      {
          scanf("%d",&a[i]);
          add(i,i+n*n,1,-a[i]);
          add(i,i+n*n,INF,0);

      }
      for(int i=1;i<=n*n;i++)
      {
          if(i%n!=0)
          add(i+n*n,i+1,INF,0);
          if((i+n-1)/n<n)
            add(i+n*n,i+n,INF,0);
      }
      add(0,1,k,0);
      add(2*n*n,2*n*n+1,k,0);
      printf("%d\n",-MCMF(0,2*n*n+1));


  }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值