poj3422Kaka's Matrix Travels【最大费用流】

来源:互联网 发布:淘宝半价抢购是真的吗 编辑:程序博客网 时间:2024/06/16 18:40

Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9402 Accepted: 3819

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels withSUM = 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 toSUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximumSUM he can obtain after his Kth travel. Note the SUM is accumulative during theK 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

题意:给出n*n矩阵,每个点上有价值,只能往右or下走一格,只能采一次价值(可以重复走,起点不定)走m次最多的价值。

做法:和

hdu4862Jump【最大费用最大流,判断是否满流】2014多校联合

几乎一样==

用流量限制m次,所以s->s'流量是m cost=0,能走的j'->i连两条边:cap=1,cost=x;cap=oo,cost=0,1其他边是cap=oo cost=0

/**********poj3422768K204MSC++2766B2016-08-31 11:19:40***********/#include <iostream>#include <cstdio>using namespace std;const int oo=1e9;//无穷大const int maxm=100000;//边的最大数量,为原图的两倍const int maxn=10000;//点的最大数量int node,src,dest,edge;//node节点数,src源点,dest汇点,edge边数int head[maxn],p[maxn],dis[maxn],q[maxn],vis[maxn];//head链表头,p记录可行流上节点对应的反向边,dis计算距离struct edgenode{    int to;//边的指向    int flow;//边的容量    int cost;//边的费用    int next;//链表的下一条边} edges[maxm];void prepare(int _node,int _src,int _dest);void addedge(int u,int v,int f,int c);bool spfa();inline int min(int a,int b){    return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){    node=_node;    src=_src;    dest=_dest;    for (int i=0; i<node; i++)    {        head[i]=-1;        vis[i]=false;    }    edge=0;}void addedge(int u,int v,int f,int c){    edges[edge].flow=f;    edges[edge].cost=c;    edges[edge].to=v;    edges[edge].next=head[u];    head[u]=edge++;    edges[edge].flow=0;    edges[edge].cost=-c;    edges[edge].to=u;    edges[edge].next=head[v];    head[v]=edge++;}bool spfa(){    int i,u,v,l,r=0,tmp;    for (i=0; i<node; i++) dis[i]=oo;    dis[q[r++]=src]=0;    p[src]=p[dest]=-1;    for (l=0; l!=r; ((++l>=maxn)?l=0:1))    {        for (i=head[u=q[l]],vis[u]=false; i!=-1; i=edges[i].next)        {            if (edges[i].flow&&dis[v=edges[i].to]>(tmp=dis[u]+edges[i].cost))            {                dis[v]=tmp;                p[v]=i^1;                if (vis[v]) continue;                vis[q[r++]=v]=true;                if (r>=maxn) r=0;            }        }    }    return p[dest]>=0;}int spfaflow(){    int i,ret=0,delta;    while (spfa())    {        //按记录原路返回求流量        for (i=p[dest],delta=oo; i>=0; i=p[edges[i].to])        {            delta=min(delta,edges[i^1].flow);        }        for (int i=p[dest]; i>=0; i=p[edges[i].to])        {            edges[i].flow+=delta;            edges[i^1].flow-=delta;        }        ret+=delta*dis[dest];    }    return ret;}int main(){//    freopen("cin.txt","r",stdin);    int n,m;    while(~scanf("%d%d",&n,&m))    {        prepare(2*n*n+3,2*n*n+2,2*n*n+1);        addedge(2*n*n+2,0,m,0);      //  addedge(2*n*n,dest,oo,0);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                int x;                scanf("%d",&x);                addedge((i-1)*n+j,(i-1)*n+j+n*n,oo,0);                addedge((i-1)*n+j,(i-1)*n+j+n*n,1,-x);                if(i<n)addedge((i-1)*n+j+n*n,(i)*n+j,oo,0);                if(j<n)addedge((i-1)*n+j+n*n,(i-1)*n+j+1,oo,0);                addedge(0,(i-1)*n+j,oo,0);                addedge((i-1)*n+j+n*n,n*n*2+1,oo,0);            }        }        printf("%d\n",-spfaflow());    }    return 0;}


0 0
原创粉丝点击