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

2023-10-30 03:58

本文主要是介绍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;
}

这篇关于Contest 2050 and Codeforces Round #718 D. Explorer Space(dp动态规划)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/305428

相关文章

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Java使用POI-TL和JFreeChart动态生成Word报告

《Java使用POI-TL和JFreeChart动态生成Word报告》本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码... 目录前言一、需求背景二、方案分析三、 POI-TL + JFreeChart 实现3.1 Maven

Java导出Excel动态表头的示例详解

《Java导出Excel动态表头的示例详解》这篇文章主要为大家详细介绍了Java导出Excel动态表头的相关知识,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录前言一、效果展示二、代码实现1.固定头实体类2.动态头实现3.导出动态头前言本文只记录大致思路以及做法,代码不进

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

SpringBoot实现动态插拔的AOP的完整案例

《SpringBoot实现动态插拔的AOP的完整案例》在现代软件开发中,面向切面编程(AOP)是一种非常重要的技术,能够有效实现日志记录、安全控制、性能监控等横切关注点的分离,在传统的AOP实现中,切... 目录引言一、AOP 概述1.1 什么是 AOP1.2 AOP 的典型应用场景1.3 为什么需要动态插

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情