PKU 3422 网络流

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

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
每个点拆成两个1,1‘!建两条边 一条为容量为1,花费为-data,另一条为容量无限,花费为0;
然后1’到右边和下面的点各连一条边  容量无限,花费为0;
#include<iostream>
#include<queue>
#include<memory>
using namespace std;
#define inf 0x7ffffff
const int N=6000;
struct FUCK
{
 int to,flow,cost;
 int next;
}node[20000];
int dia[60*60];
int n,tot,k,en;
int pre[N];
int dis[N];
bool flag[N];
int cnt[N];
int road[N];
int s,t;
int SPFA()
{
 queue<int>Q;
 int now,next;
 pre[0] = 0;
 for(int i=1;i<=t;i++)
  dis[i] = inf;
 dis[0] = 0;
 memset(flag,false,sizeof(flag));
 flag[s] = true;
 Q.push(s);
 while(!Q.empty())
 {
  now = Q.front();
  Q.pop();
  flag[now] = false;
  next=cnt[now];
  while(next!=-1)
  {
   if(node[next].flow>0&& dis[node[next].to] > dis[now] + node[next].cost)
   {
    dis[node[next].to] = dis[now] + node[next].cost;
    pre[node[next].to] = now;
    road[node[next].to]=next;
    if(!flag[node[next].to])
    {
     Q.push(node[next].to);
     flag[node[next].to] = true;
    }
   }
   next=node[next].next;
  }
 }
 if(dis[t] != inf)
  return 1;
 else
  return 0;
}
int mincostflow()
{
 int last,min,ans=0;
 while(SPFA())
 {
  min=inf;
  last=t;
  while(last!=s)
  {
   if(node[road[last]].flow<min)
    min=node[road[last]].flow;
   last=pre[last];
  }
  ans+=min*dis[t];
  last=t;
  while(last!=s)
  {
   node[road[last]].flow-=min;
   node[road[last]^1].flow+=min;
   last=pre[last];
  }
 }
 return ans;
}
void addedge(int from,int to,int cost,int flow)
{
 node[en].flow=flow;
 node[en].cost=cost;
 node[en].to=to;
 node[en].next=cnt[from];
 cnt[from]=en++;
}
void add(int from,int to,int cost,int flow)
{
 addedge(from,to,cost,flow);
 addedge(to,from,-cost,0);
}
int main()
{
 int i;
 while(scanf("%d%d",&n,&k)!=EOF)
 {
  tot=n*n;
  for(i=1;i<=tot;i++)
   scanf("%d",&dia[i]);
  en=0;
  memset(cnt,-1,sizeof(cnt));
  s=0;
  t=tot+tot+1;
  for(i=1;i<=tot;i++)
  {
   add(i,i+tot,-dia[i],1);
   add(i,i+tot,0,inf);
   if(i%n!=0)
    add(i+tot,i+1,0,inf);
   if(i+n<=tot)
    add(i+tot,i+n,0,inf);
  }
  add(s,1,0,k);
  add(tot+tot,t,0,k);
  printf("%d/n",-mincostflow());
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值