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

相关文章

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

golang实现动态路由的项目实践

《golang实现动态路由的项目实践》本文主要介绍了golang实现动态路由项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习... 目录一、动态路由1.结构体(数据库的定义)2.预加载preload3.添加关联的方法一、动态路由1

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL