Codeforces Global Round 13 B - Minimal Cost

8 篇文章 0 订阅

传送门
B. Minimal Cost
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

思路:

贪心,如果相邻两行的障碍物在同一列的话,就比较障碍物横向走两格和横纵走一格所花硬币的数量,如果相邻两行的障碍物横坐标差距为1,就比较纵和横所花硬币的数量,如果相邻两行的障碍物横坐标差距大于1,ans = 0;

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
#define ll long long                     
long long a[110];
int main()
{
	ll t;
	scanf("%lld",&t);
	while(t--)
	{
		memset(a,0,sizeof(a));
		ll n,u,v;
		scanf("%lld%lld%lld",&n,&u,&v);
		for(int i = 1; i <= n; i++)
		{
			ll x;
			scanf("%lld",&x);
			a[i]= x;
		}
		ll ans = 1e18;
		for(int i = 2; i <= n; i++)
		{
				if(a[i] == a[i-1])
				{
					ans = min(ans,min(v+v, u+v));
				}
				else if(abs(a[i] - a[i-1]) == 1)
				{
					ans = min(ans,min(u, v ));
				}
				else
				{
					ans = 0;
				}
		}
		printf("%lld\n",ans);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值