Contest 2050 and Codeforces Round #718 D. Explorer Space(dp动态规划)

题目连接:https://codeforces.com/contest/1517/problem/D

You are wandering in the explorer space of the 2050 Conference.

The explorer space can be viewed as an undirected weighted grid graph with size n×m. The set of vertices is {(i,j)|1≤i≤n,1≤j≤m}. Two vertices (i1,j1) and (i2,j2) are connected by an edge if and only if |i1−i2|+|j1−j2|=1.

At each step, you can walk to any vertex connected by an edge with your current vertex. On each edge, there are some number of exhibits. Since you already know all the exhibits, whenever you go through an edge containing x exhibits, your boredness increases by x.

For each starting vertex (i,j), please answer the following question: What is the minimum possible boredness if you walk from (i,j) and goes back to it after exactly k steps?

You can use any edge for multiple times but the boredness on those edges are also counted for multiple times. At each step, you cannot stay on your current vertex. You also cannot change direction while going through an edge.

Input
The first line contains three integers n, m and k (2≤n,m≤500,1≤k≤20).

The j-th number (1≤j≤m−1) in the i-th line of the following n lines is the number of exibits on the edge between vertex (i,j) and vertex (i,j+1).

The j-th number (1≤j≤m) in the i-th line of the following n−1 lines is the number of exibits on the edge between vertex (i,j) and vertex (i+1,j).

The number of exhibits on each edge is an integer between 1 and 106.

Output
Output n lines with m numbers each. The j-th number in the i-th line, answerij, should be the minimum possible boredness if you walk from (i,j) and goes back to it after exactly k steps.

If you cannot goes back to vertex (i,j) after exactly k steps, answerij should be −1.

Examples

input

3 3 10
1 1
1 1
1 1
1 1 1
1 1 1

output

10 10 10
10 10 10
10 10 10

input

2 2 4
1
3
4 2

output

4 4
10 6

input

2 2 3
1
2
3 4

output

-1 -1
-1 -1

分析

将每个方格用其序号表示,(i, j) 即序号 (i - 1) * m + j 。
设 dp[i][j] 表示从第 i 个方格走出去 j 步最少的花费,转移方程为:
dp[i][j] = min(dp[i][j], dp[与 i 相邻的方格][j - 1] + 到每格的花费)

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int n,m,k;
map<int,int> g[250010];
int dp[250010][15];
int dirx[4]={0,1,0,-1};
int diry[4]={1,0,-1,0};
 
int main(){
	memset(dp, 0x3f3f3f3f, sizeof(dp));
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n*m;i++) dp[i][0] = 0;
    for(int i=1;i<=n;i++)
    	for(int j=1;j<m;j++)
    	{
    		int x;
    		scanf("%d",&x);
    		int u = (i - 1) * m + j;
    		int v = u + 1;
    		g[u][v] = g[v][u] = x;
		}
	for(int i=1;i<n;i++)
    	for(int j=1;j<=m;j++)
    	{
    		int x;
    		scanf("%d",&x);
    		int u = (i - 1) * m + j;
    		int v = u + m;
    		g[u][v] = g[v][u] = x;
		}
	if(k % 2 != 0)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				printf("-1 ");
			}
			printf("\n");
		}
		return 0;
	}
	for(int kk=1;kk<=k/2;kk++)
		for(int x=1;x<=n;x++)
	    	for(int y=1;y<=m;y++)
	    	{
	    		int u,v;
	    		u = (x - 1) * m + y;
	    		for(int i=0;i<4;i++)
	    		{
	    			int xx = x + dirx[i];
	    			int yy = y + diry[i];
	    			if(xx >= 1 && xx <= n && yy >= 1 && yy <= m)
	    			{
	    				v = (xx - 1) * m + yy;
	    				dp[u][kk] = min(dp[u][kk], dp[v][kk-1] + g[u][v]);
					}
				}
			}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			int u = (i - 1) * m + j;
			printf("%d ",dp[u][k/2]*2);
		}
		printf("\n");
	}
    
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值