Minimal Cost

2024-01-04 08:38
文章标签 cost minimal

本文主要是介绍Minimal Cost,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:
There is a graph of n rows and 106+2 columns, where rows are numbered from 1 to n and columns from 0 to 106+1:

Let’s denote the node in the row i and column j by (i,j).

Initially for each i the i-th row has exactly one obstacle — at node (i,ai). You want to move some obstacles so that you can reach node (n,106+1) from node (1,0) by moving through edges of this graph (you can’t pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:

If there is an obstacle in the node (i,j), you can use u coins to move it to (i−1,j) or (i+1,j), if such node exists and if there is no obstacle in that node currently.
If there is an obstacle in the node (i,j), you can use v coins to move it to (i,j−1) or (i,j+1), if such node exists and if there is no obstacle in that node currently.
Note that you can’t move obstacles outside the grid. For example, you can’t move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.

Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n,106+1) from node (1,0) by moving through edges of this graph without passing through obstacles.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains three integers n, u and v (2≤n≤100, 1≤u,v≤109) — the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤106) — where ai represents that the obstacle in the i-th row is in node (i,ai).

It’s guaranteed that the sum of n over all test cases doesn’t exceed 2⋅104.

Output
For each test case, output a single integer — the minimal number of coins you need to spend to be able to reach node (n,106+1) from node (1,0) by moving through edges of this graph without passing through obstacles.

It can be shown that under the constraints of the problem there is always a way to make such a trip possible.

Example
inputCopy
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
outputCopy
7
3
3
Note
In the first sample, two obstacles are at (1,2) and (2,2). You can move the obstacle on (2,2) to (2,3), then to (1,3). The total cost is u+v=7 coins.

In the second sample, two obstacles are at (1,3) and (2,2). You can move the obstacle on (1,3) to (2,3). The cost is u=3 coins
题解:
每行只有一个障碍物,并且障碍物不在第一列和最后一列,所以分三种情况
1:有一行的障碍物距离大于等于2,则不需要挪动
2:障碍物都在同一列,则需要挪动u+v
3:不都在同一列,则可以u+v或者同行移动两格

#include <bits/stdc++.h>
using namespace std;
long long a[105];
int main()
{int t;cin>>t;while(t--){long long n,u,v;cin>>n>>u>>v;int ret=1;for(int i=1;i<=n;i++){scanf("%lld",&a[i]);if(a[i]!=a[i-1]&&i!=1) ret=0;}long long ans=0;int f=0;for(int i=1;i<=n;i++){if(abs(a[i]-a[i+1])>=2&&i!=n){ans=0;f=1;break;}}if(!f){if(ret) ans=min(u+v,2*v);else ans=min(u,v);}cout<<ans<<endl;}return 0;
}

这篇关于Minimal Cost的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

【服务器运维】CentOS6 minimal 离线安装MySQL5.7

1.准备安装包(版本因人而异,所以下面的命令中版本省略,实际操作中用Tab自动补全就好了) cloog-ppl-0.15.7-1.2.el6.x86_64.rpmcpp-4.4.7-23.el6.x86_64.rpmgcc-4.4.7-23.el6.x86_64.rpmgcc-c++-4.4.7-23.el6.x86_64.rpmglibc-2.12-1.212.el6.x86_64.r

【服务器运维】CentOS7 minimal 离线安装 gcc perl vmware-tools

0. 本机在有网的情况下,下载CentOS镜像 https://www.centos.org/download/ 1. 取出rpm 有的情况可能不需要net-tools,但是如果出现跟ifconfig相关的错误,就把它安装上。另外如果不想升级内核版本的话,就找对应内核版本的rpm版本安装 perl-Time-Local-1.2300-2.el7.noarch.rpmperl-Tim

【杂记-浅谈动态路由中的Cost花销值】

一、Cost概述 动态路由协议中的Cost值是衡量路由优劣程度的一个重要参数,它影响了路由选择的过程。在不同的动态路由协议中,Cost值的计算方式可能会有所差异,但它们共同的目标是为了找到最佳路径,使得数据包能够在网络中高效传输。 动态路由协议中的Cost值是路由选择的关键参数,它综合了多个因素来衡量路由的质量。不同的协议有着不同的Cost计算方式,但都是为了找到最佳路径,提高网络传输的效率和可

Minimal CentOS安装VMwareTools

最简版的CentOS很多东西都没有,为了便于linux与客户机的交互,虚拟机会提示安装VMwareTools,确认登录客户机,挂载CD驱动器,然后解压tar压缩包,运行vmware-install.pl安装VMwareTools。具体怎么做呢? 以root身份登录系统; vmware-tools安装存在几个依赖,保证依赖包都安装完毕 依次执行如下命令 # yum install -y

Ural 1303. Minimal Coverage / 最小区间覆盖

求最小区间覆盖0-m 以前做过 现在墨迹半天写出来 弱爆了 像这样的1 9 和 2 7 根据贪心原理后者不需要直接去掉 然后按照起点从小到大排序 在按照终点从大到小排序  贪心模拟一下每次能不选就不选 (1 6)  (1 5)  (2 9)   (3 10)   (7 10)选择(1 6) 之后 下一个选择是(3 10) 他是最后一个能选的  不选就会断开 并且比选(2 9)更优 会不会

RT-DETR 详解之 Uncertainty-minimal Query Selection

引言 在上一章博客中博主已经完成查询去噪向量构造部分的讲解(DeNoise)在本篇博客中,我们将进行Uncertainty-minimal Query Selection创新点的讲解。 Uncertainty-minimal Query Selection是RT-DETR提出的第二个创新点,其作用是在训练期间约束检测器对高 IOU 的特征产生高分类分数,对低 IOU 的特征产生低分类分数。从而

6.11 Libbpf-bootstrap(二,Minimal)

写在前面 minimal是一个很好的入门示例。可以将其视为一个简单的POC,用于尝试BPF功能。它不使用BPF CO-RE,因此可以使用较旧的内核,并且只需包含系统内核头文件即可获取内核类型定义。这不是构建生产就绪应用程序和工具的最佳方法,但对于本地实验来说已经足够了。 一,BPF侧 minimal.bpf.c// SPDX-License-Identifier: GPL-2.0 OR B

POJ 3925 Minimal Ratio Tree(枚举+最小生成树)

POJ 3925 Minimal Ratio Tree 题目链接 题意:给定一些点权和一个边权矩阵,求一个最小的比例的树 思路:先枚举用哪些点,然后求最小生成树即可 代码: #include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 20;int n

Codeforces Round #228 (Div. 2) D - Fox and Minimal path

前三题 没有什么可以说的 水题, 但是 B题 大意了,最后WA了,所以 rating 大降 T_T.  何时才能进DIV1 啊。  D题, 当时 交了一发,不过被HACK了,因为考虑不全面。 当时的想法是把 K 分解为1000 内的因数相乘 而且 这些因数的和<=1000。这样显然是不正确的,因为可能根本找不到这些符合条件的因数。 之后,考虑分解为二进制,之后是相乘之积 相加。上面的错误之处

索引问题引起的执行计划偏移(EBS Cost Management performance issue )

之前遇到的一个EBS性能问题,更新至CSDN Create Accounting - Cost Management_performance issue SYMPTOMS 元旦过后,create accounting cost management执行时间过长,还时常报错,影响正常作业; 1.对比2018/12和2019/01的报表执行时间,确实能看到 Accounting Progra