当前位置:网站首页>最低通行费
最低通行费
2022-06-10 00:11:00 【芜湖男童】
题目描述
一个商人穿过一个 N × N N×N N×N 的正方形的网格,去参加一个非常重要的商务活动。
他要从网格的左上角进,右下角出。
每穿越中间 1 个小方格,都要花费 1 个单位时间。
商人必须在 ( 2 N − 1 ) (2N−1) (2N−1) 个单位时间穿越出去。
而在经过中间的每个小方格时,都需要缴纳一定的费用。
这个商人期望在规定时间内用最少费用穿越出去。
请问至少需要多少费用?
注意:不能对角穿越各个小方格(即,只能向上下左右四个方向移动且不能离开网格)。
输入格式
第一行是一个整数,表示正方形的宽度 N N N。
后面 N N N 行,每行 N N N 个不大于 100 的正整数,为网格上每个小方格的费用。
输出格式
输出一个整数,表示至少需要的费用。
数据范围
1 ≤ N ≤ 100 1≤N≤100 1≤N≤100
样例
输入样例
5
1 4 6 8 10
2 5 7 15 17
6 8 9 18 20
10 11 12 19 21
20 23 25 29 33
输出样例
109
样例解释
样例中,最小值为 109=1+2+5+7+9+12+19+21+33。
思路
本题和数字三角形类型相似,但是有一点不同,是求 最 小 值 \color{Red}{最小值} 最小值,所以需要考虑初始化的状态以及状态转移时的越界判断
C++ 代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 110, INF = 1e9;
int a[N][N], f[N][N];
int n;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
scanf("%d", &a[i][j]);
// 初始化为正无穷
memset(f,0x3f,sizeof f);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
{
if (i == 1 && j == 1) f[i][j] = a[i][j];
else f[i][j] = min(f[i - 1][j], f[i][j - 1]) + a[i][j];
}
printf("%d", f[n][n]);
return 0;
}
边栏推荐
- C # WPF realizes dynamic increase and decrease of tab page
- MySQL execution plan
- Several syntax for adding events to elements in a page
- If (obj! = null) is not required
- SpingBoot+Quartrz集群版实现动态定时任务(利用反射实现自定义业务)
- Johns Hopkins, Berkeley, California, etc. | using machine learning of sequence and structural features to achieve Generalized Prediction of antibody thermal stability
- 力扣 两数之和 C语言 题解
- 蓝桥杯·寒假百校真题大联赛(第五期)研究生&大学A组 《货物摆放》真题练习
- Performance optimization scheme
- How to keep the contents of WPS merged cells in one cell
猜你喜欢

SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统

Johns Hopkins, Berkeley, California, etc. | using machine learning of sequence and structural features to achieve Generalized Prediction of antibody thermal stability

wps合并单元格后怎么将文字的位置进行调整

ArcMap resolving geometric errors

力扣 无重复字符的最长子串 C语言 题解

Where is the precise position of PS ruler adjustment

Niuke.com: median in data flow
力扣 旋转字符串 C语言 题解

工藤正男:如何一年发表5篇SCI

Illustration Google V8 06: prototype chain: how does V8 implement object inheritance?
随机推荐
低边驱动和高边驱动
冒泡排序及优化 清楚直观 C语言
wps文本框如何设置透明
Cloudcompare & PCL principal curvature, mean curvature and Gaussian curvature calculation
wps怎么合并大小不一致的单元格
数字大时代来临,360携手创业黑马助力中小企业抓住关键未来
Pta7-6 quietly follows
JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax
不用 if (obj != null) 判空
請問徽商期貨是正規的嗎?開戶交易安全嗎?
View the installable version number of the wheel on this computer
10 common high-frequency business scenarios that trigger IO bottlenecks
js 邏輯空分配雙問號語法 、雙豎杠語法 與 可選鏈語法
Encryption, decryption and signature verification
约翰霍普金斯、加州伯克利等 | 利用序列和结构特征的机器学习实现抗体热稳定性的泛化预测
二分查找(折半查找)总结
Automatically open personal when opening xlsx file Xlsb table file
Baidu post-95 programmers were sentenced for deleting databases and running away. The motivation was the change of work content and dissatisfaction with leaders. What impact would deleting databases b
PTP授时服务器(NTP网络时间服务器)技术方案应用
DDD driven domain design learning notes