POJ3422 Kaka's Matrix Travels 最小费用流

来源:互联网 发布:mpu9250 九轴融合算法 编辑:程序博客网 时间:2024/05/01 06:06
Kaka's Matrix TravelsTime Limit: 1000MS      Memory Limit: 65536KTotal Submissions: 9608     Accepted: 3911DescriptionOn 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.InputThe 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.OutputThe maximum SUM Kaka can obtain after his Kth travel.Sample Input3 21 2 30 2 11 4 2Sample Output15SourcePOJ Monthly--2007.10.06, Huang, Jinsong

每个方格表示为一个点
设第i个方格的数字为w[i]
拆点
点i 连一条 费用为-w[i],容量为1 的边 到i’ –>表示只能取一次数 费用为负求最小费用 最终结果取反即可得最大费用
再连一条 费用为0 ,容量为inf 的边 到i’ —>取完一次后 走这条边

S到点1 容量为k 费用为0
点n*n带T 容量为 k 费用为0

#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>const int N = 5500;const int M = N*10;struct Edge{    int to,c,w,next;}edge[M];int head[N];int nume=0;inline void addEdge(int k,int u,int v,int c,int w){    edge[k].to=v,            edge[k].c=c,            edge[k].w=w,            edge[k].next=head[u];    head[u]=k;}inline void addEdge(int u,int v,int c,int w){    addEdge(nume++,u,v,c,w);    addEdge(nume++,v,u,0,-w);}void init(int n){    fill(head,head+n+1,-1);    nume=0;}bool used[N];int dis[N],load[N],p[N];//距离 ,前驱边,前驱点bool spfa(int s,int e,int n){    deque<int>que;    for(int i=0;i<=n;++i){        dis[i]=INF;        load[i]=p[i]=-1;        used[i]=false;    }    que.push_back(s);    dis[s]=0;    used[s]=true;    while(!que.empty()){        int u=que.front();        que.pop_front();        used[u]=false;        for(int i=head[u];i!=-1;i=edge[i].next){            if(edge[i].c>0){                int v=edge[i].to;                if(dis[v]>dis[u]+edge[i].w){                    dis[v]=dis[u]+edge[i].w;                    p[v]=u;                    load[v]=i;                    if(used[v]==false){                        used[v]=true;                        que.push_back(v);                    }                }            }        }    }    return dis[e]!=INF;}int min_cost_flow(int s,int t,int n){    int ansflow=0,anscost=0;    while(spfa(s,t,n)){        int u=t;        int f=INF;        while(p[u]!=-1){            f=min(f,edge[load[u]].c);            u=p[u];        }        u=t;        while(p[u]!=-1){            edge[load[u]].c-=f;            edge[load[u]^1].c+=f;            u=p[u];        }        anscost+=dis[t]*f;        ansflow+=f;    }    return anscost;}inline int myHash(int i,int j,int n){    return (i-1)*n+j;}void slove(int n,int k){    int s=2*n*n+1,t=s+1;    init(2*n*n+2);    for(int i=1,w;i<=n;++i){        for(int j=1;j<=n;++j){            scanf("%d",&w);            int u=myHash(i,j,n);            addEdge(u,u+n*n,1,-w);//拆点            addEdge(u,u+n*n,INF,0);            if(i!=n){                int v=myHash(i+1,j,n);                addEdge(u+n*n,v,INF,0);            }            if(j!=n){                int v=myHash(i,j+1,n);                addEdge(u+n*n,v,INF,0);            }        }    }    addEdge(s,1,k,0);    addEdge(2*n*n,t,k,0);    printf("%d\n",-min_cost_flow(s,t,2*n*n+2));}int main(){    //freopen("/home/lu/Documents/r.txt","r",stdin);    //freopen("/home/lu/Documents/w.txt","w",stdout);    int n,k;    while(~scanf("%d%d",&n,&k)){        slove(n,k);    }    return 0;}
0 0
原创粉丝点击