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

Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total 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 2
1 2 3
0 2 1
1 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值