D. Explorer Space 经典DP

阅读: 评论:0

D. Explorer Space 经典DP

D. Explorer Space 经典DP

D. Explorer Space

time limit per test: 2 seconds
memory limit per test: 256 megabytes
inpu: tstandard input
output: standard output

  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 go 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. Before going back to your starting vertex (i,j) after k steps, you can visit (i,j) (or not) freely.

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 go back to it after exactly k steps.

  If you cannot go 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

Note

  In the first example, the answer is always 10 no matter how you walk.

  In the second example, answer21=10, the path is (2,1)→(1,1)→(1,2)→(2,2)→(2,1), the boredness is 4+1+2+3=10.

Code

#include<bits/stdc++.h>
#define ll int
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;
string s1, s2, s;
int main()
{FAST_IO;cin >> n >> m >> k;ll h[n + 1][m], l[n][m + 1];vector<vector<ll> >re ( n + 1, vector<ll> ( m + 1, 0 ) );vector<vector<ll> >temp ( n + 1, vector<ll> ( m + 1, 0 ) );for ( i = 1; i <= n; i++ )for ( j = 1; j < m; j++ )cin >> h[i][j];for ( i = 1; i < n; i++ )for ( j = 1; j <= m; j ++ )cin >> l[i][j];for ( t = 1; t <= k / 2; t++ ){for ( i = 1; i <= n; i++ ){for ( j = 1; j <= m; j++ ){temp[i][j] = ( int ) 1e9;;if ( i != n )temp[i][j] = min ( temp[i][j], re[i + 1][j] + l[i][j] * 2 );if ( i != 1 )temp[i][j] = min ( temp[i][j], re[i - 1][j] + l[i - 1][j] * 2 );if ( j != m )temp[i][j] = min ( temp[i][j], re[i][j + 1] + h[i][j] * 2 );if ( j != 1 )temp[i][j] = min ( temp[i][j], re[i][j - 1] + h[i][j - 1] * 2 );}}swap ( re, temp );}for ( i = 1; i <= n; cout << endl, i++ )for ( j = 1; j <= m; j++ ){if ( j > 1 ) cout << " ";cout << ( k % 2 ? -1 : re[i][j] );}}

本文发布于:2024-02-04 06:05:09,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170700633252931.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:经典   Explorer   Space   DP
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23