CF1517.D---dp

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.

cf题目

题目大意:
在坐标系内,可以进行水平和竖直移动,每一步移动一格
每相邻格之间有加权边连接,每次通过此边总cost就会加上边权,
求对于每格(为原点)开始,移动k步之后返回原位总cost的最小值

题解:
dp,这题比较无脑,但是我今天下午在图书馆看不进去机器学习了也不想做题所以来水一波题解,
状态转移公式
dp[i][j][k]=min(dp[i+dx][j+dy][k-2]+cost[pos0][pos1]*2)

具体见代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define me(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long ll;
const ll N = 510, inf = 1e12;
ll cost[N][N][4], dp[N][N][30];
ll n, m, K;
ll dir[4][2] = { -1,0,0,-1,1,0,0,1 };

void floyd() {
	for (ll k = 2; k <= K; k++) {
		for(ll i=1;i<=n;i++)
			for (ll j = 1; j <= m; j++) {
				dp[i][j][k] = inf;
				for (ll d = 0; d < 4; d++) {
					ll dx = i + dir[d][0], dy = j + dir[d][1];
					if (dx<1 || dx>n || dy<1 || dy>m)continue;
					dp[i][j][k] = min(dp[i][j][k], dp[dx][dy][k - 2]+2*cost[i][j][d]);
				}
			}
	}
}

int main() {
	cin >> n >> m >> K;
	for(ll i=1;i<=n;i++)
		for (ll j = 1; j <= m - 1; j++) {
			cin >> cost[i][j][3];
			cost[i][j + 1][1] = cost[i][j][3];
		}
	for(ll i=1;i<=n-1;i++)
		for (ll j = 1; j <= m; j++) {
			cin >> cost[i][j][2];
			cost[i + 1][j][0] = cost[i][j][2];
		}
	if (K & 1) {
		for (ll i = 1; i <= n; i++) {
			for (ll j = 1; j <= m; j++)printf("%lld ", -1ll);
			cout << "\n";
		}
		return 0;
	}
	floyd();
	for (ll i = 1; i <= n; i++) {
		for (ll j = 1; j <= m; j++)printf("%lld ", dp[i][j][K]);
		cout << "\n";
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值