poj 3422 Kaka's Matrix Travels(费用流,经典构图)

来源:互联网 发布:社会网络信息流模型 编辑:程序博客网 时间:2024/06/05 23:05
Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9285 Accepted: 3759

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


思路:最小费用最大流。建图很重要,这里用到拆点,将每个点拆成两个,这两点之间连两条边,一条容量为1,费用为该节点的值,另一条边容量为无穷或k,费用为0,这样保证就算经过这点k次时,费用也只被计算一次。由于每个点只能往右或者往下走,所以将它和右边及下边的点连一条边,容量为无穷,费用为0。源点向第一个点连边,容量为k,费用为0,最后一个点向汇点连边,容量为k,费用为0。

关于费用流的问题,如果一个点需要走多次,但只能取一次费用,可以建立多条边,其中一条边的费用为c,其它都为0,这样可以保证首先肯定要经过费用为c的边,如果还能继续走,由于这条边已经满流了,所以必定走费用为0的边,这样就可以做到经过k次点,但费用只记一次。

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int maxn = 50005;const int inf = 0x3f3f3f3f;struct Edge{int from,to,flow,cost;Edge(){}Edge(int f,int t,int fl,int co):from(f),to(t),flow(fl),cost(co){}  };struct MCMF{int n,s,t;vector<Edge> edge;vector<int> G[maxn];int dis[maxn];int pre[maxn];bool inq[maxn];void init(int n,int s,int t){this->n = n, this->s = s, this->t = t;edge.clear();for(int i = 1; i <= n; i++) G[i].clear();}void addedge(int u,int v,int flow,int cost){edge.push_back(Edge(u,v,flow,cost));edge.push_back(Edge(v,u,0,-cost));int m = edge.size();G[u].push_back(m-2);G[v].push_back(m-1);}int spfa(){queue<int> q;memset(dis,-1,sizeof(dis));memset(pre,-1,sizeof(pre));memset(inq,false,sizeof(inq));dis[s] = 0;inq[s] = true;q.push(s);while(!q.empty()){int u = q.front();q.pop();inq[u] = false;for(int i = 0; i < G[u].size(); i++){int v = edge[G[u][i]].to;if(dis[v] < dis[u] + edge[G[u][i]].cost && edge[G[u][i]].flow > 0){dis[v] = dis[u] + edge[G[u][i]].cost;pre[v] = G[u][i];if(inq[v] == false){inq[v] = true;q.push(v);}}}}return dis[t] != -1;}int solve(){int maxcost = 0,minflow;while(spfa()){minflow = inf;for(int i = pre[t]; i != -1; i = pre[edge[i].from])minflow = min(minflow,edge[i].flow);for(int i = pre[t]; i != -1; i = pre[edge[i].from]){edge[i].flow -= minflow;edge[i^1].flow += minflow;}maxcost += dis[t] * minflow;}return maxcost;}}MM;int n,m,mat[55][55];int main(){while(scanf("%d%d",&n,&m)!=EOF){for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)scanf("%d",&mat[i][j]);int s = 0,t = n * n * 2 + 1;MM.init(n * n * 2 + 2,s,t);MM.addedge(s,1,m,0);for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++){MM.addedge((i-1)*n+j,(i-1)*n+j+n*n,1,mat[i][j]);MM.addedge((i-1)*n+j,(i-1)*n+j+n*n,inf,0);if(i + 1 <= n)MM.addedge((i-1)*n+j+n*n,i*n+j,inf,0);if(j + 1 <= n)MM.addedge((i-1)*n+j+n*n,(i-1)*n+j+1,inf,0);}MM.addedge(n * n * 2,t,m,0);printf("%d\n",MM.solve());}return 0;}


0 0
原创粉丝点击