POJ--Kaka's Matrix Travels(最大费用最大流 && 经典建图 && 好题)

来源:互联网 发布:js 移动端 上传照片 编辑:程序博客网 时间:2024/06/13 21:58
传送门:Kaka's Matrix Travels
描述:
Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8741 Accepted: 3504

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

题意:

给你一个N*N的矩阵,每个位置都有一定的点权,当你走到一个位置时,你可以获取该位置的点权。现在一个人要从左上角到右下角走K次,每次只能选择向下走或者向右走,问你走K次所能获得的最大权值和。 要求——每个点可以无限走,但点权只能获取一次。


思路:

一个点可以走多次,点权只能获取一次,也就是说我们走到这一点不一定要获得这一点的点权,不同于HDU3376,这点要注意。

还是把每个点 i 拆成 左点 i 和右点 i。建立超级源点,超级汇点。

建图步骤如下:

(1)每个点的 左点连接右点, 流量为1, 费用为点的权值。

(2)每对可达点之间建边, 如 u -->v ,需要建4条边:左u -> 左v,右u -> 左v,左u -> 右v,右u -> 右v。流量为INF(因为我们可以任意选择经过该边的流量(不超过K的前提下),只要它能使最权值和最大化就行了),费用为0。(大家可以模拟一下)。

(3)源点到左起点建边,流量为k,费用为0, 右终点到汇点建边,流量为k,费用为0。

具体为什么这样建边的,我就不多做解释了,大家可以看小比博客,他写的已经够详细了,还不太清楚的可以模拟画一下图。

代码:

#include <iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;//最小费用大流,求只需要取相反数结果即可。//点的总数为 N,点的编号 0~N -1const int maxn=610*610*2+2;const int maxm=4*maxn;const int inf=0x3f3f3f3f;struct Edge{  int to,next,cap,flow,cost;}es[maxm];int head[maxn],tol;int p[maxn];//上一条弧int d[maxn];//单位总费用bool vis[maxn];int n,k;int N;//节点总个数int outset,inset;int map[610][610];void init(int n){  N=n;  tol=0;  memset(head, -1, sizeof(head));}void addedge(int u,int v,int cap,int cost){  es[tol].to=v;  es[tol].cap=cap;  es[tol].cost=cost;  es[tol].flow=0;  es[tol].next=head[u];  head[u]=tol++;  es[tol].to=u;  es[tol].cap=0;  es[tol].cost=-cost;  es[tol].flow=0;  es[tol].next=head[v];  head[v]=tol++;}bool spfa(int s,int t){  queue<int>q;  for(int i=0; i<N; i++){    d[i]=inf;    vis[i]=false;    p[i]=-1;  }  d[s]=0;  vis[s]=true;  q.push(s);  while(!q.empty()){    int u=q.front();    q.pop();    vis[u]=false;    for(int i=head[u]; i!=-1; i=es[i].next){ //逆向枚举以u为起点的边      int v=es[i].to;      if(es[i].cap>es[i].flow && d[v]>d[u]+es[i].cost){        d[v]=d[u]+es[i].cost;        p[v]=i;        if(!vis[v]){ vis[v]=true; q.push(v);}      }    }  }  if(p[t]==-1)return false;  else return true;}//返回的是最大流,cost存的是最小费用int MCMF(int s,int t,int &cost){  int flow = 0;cost = 0;  while(spfa(s,t)){//每次寻找花销最小的路径    int Min=inf;    for(int i=p[t]; i!=-1; i=p[es[i^1].to]){      if(Min>es[i].cap-es[i].flow)      Min=es[i].cap-es[i].flow;    }    //增广    for(int i=p[t]; i!=-1; i=p[es[i^1].to]){      es[i].flow+=Min;      es[i^1].flow-=Min;      cost+=es[i].cost*Min;    }    flow+=Min;  }  return flow;}int change(int x,int y){  return (x-1)*n+y;}void getmap(){  int t=n*n;  outset=0;inset=2*n*n+1;  for(int i=1; i<=n; i++){    for(int j=1; j<=n; j++){      scanf("%d",&map[i][j]);      addedge(change(i,j), change(i,j)+t, 1, -map[i][j]);      if(i<n){        addedge(change(i,j), change(i+1,j), inf, 0);        addedge(change(i,j)+t, change(i+1,j), inf, 0);        addedge(change(i,j), change(i+1,j)+t, inf, 0);        addedge(change(i,j)+t, change(i+1,j)+t, inf, 0);      }      if(j<n){        addedge(change(i,j), change(i,j+1), inf, 0);        addedge(change(i,j)+t, change(i,j+1), inf, 0);        addedge(change(i,j), change(i,j+1)+t, inf, 0);        addedge(change(i,j)+t, change(i,j+1)+t, inf, 0);      }    }  }  addedge(outset, 1, k, 0);  addedge(change(n,n)+t, inset, k, 0);}int main(){  #ifndef ONLINE_JUDGE  freopen("in.txt","r",stdin);  #endif  while(~scanf("%d%d",&n,&k)){    init(2*n*n+2);    getmap();    int cost;    MCMF(outset, inset, cost);cost=-cost;    printf("%d\n",cost);  }  return 0;}


0 0