PKU 3422 网络流

来源:互联网 发布:js的tostring方法 编辑:程序博客网 时间:2024/06/05 16:17
Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536KTotal 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 21 2 30 2 11 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;
}
原创粉丝点击