Codeforces Round #676 (Div. 2) D. Hexagons(思维)

编程入门 行业动态 更新时间:2024-10-08 04:27:53

Codeforces Round #676 (Div. 2) D. Hexagons(<a href=https://www.elefans.com/category/jswz/34/1770010.html style=思维)"/>

Codeforces Round #676 (Div. 2) D. Hexagons(思维)

题目链接:

Lindsey Buckingham told Stevie Nicks “Go your own way”. Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.

Consider a hexagonal tiling of the plane as on the picture below.

Nicks wishes to go from the cell marked (0,0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0,0) to (1,1) will take the exact same cost as going from (−2,−1) to (−1,0). The costs are given in the input in the order c1, c2, c3, c4, c5, c6 as in the picture below.

Print the smallest cost of a path from the origin which has coordinates (0,0) to the given cell.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains two integers x and y (−109≤x,y≤109) representing the coordinates of the target hexagon.

The second line of each test case contains six integers c1, c2, c3, c4, c5, c6 (1≤c1,c2,c3,c4,c5,c6≤109) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).

Output
For each testcase output the smallest cost of a path from the origin to the given cell.

Example

input

2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

output

18
1000000000000000000
题意

开始在坐标为 (0, 0) 的六边形内,要走到给出的 (x, y) 六边形,给出往每个方向走的花费,问走到目标的最小花费。

分析

我们可以发现其实这道题就是在原本只有 x 轴和 y 轴的直角坐标系的基础上又添加了一条坐标。我们将这些坐标画出来就是这样:

图里两条红色的坐标就是我们熟悉的 x 和 y 轴,蓝色的轴我们不妨叫他 z 轴,这三根轴将图划分为了六个象限。

我们举个例子:

如图,如果我们要走到坐标 (-3, -2) ,那么可能的最短的路径可能有一号和二号两条路,我们如何区分这两条路谁更优呢,可以得到:
一号路长度为:c4 + c4 + c3;
二号路长度为:c3 + c3 + c4 + c5。
c3 + c5 移动的坐标等效于 c4 ,但是他们的花费可能是不同的,所以我们可以在开始预处理出每个方向的最短花费:
val[i] = min(dir[i], dir[i-1] + dir[i+1])(dir 即 c,注意边界)

这样我们就可以简化问题,如果目标点在一、三、四、六象限,直接先从 z 轴走,再通过 x 轴或 y 轴的方向走即可。

同样的,如果目标在二或五象限,我们就可以直接忽略 z 轴的方向,直接从 x 轴和 y 轴的方向走到目标(就和直角坐标系一样了)。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;int T;
int x,y;
int val[7];
int dir[7];int main()
{scanf("%d",&T);while(T--){scanf("%d%d",&x,&y);for(int i=1;i<=6;i++)scanf("%d",&dir[i]);for(int i=1;i<=6;i++)//预处理 val[i] = min(dir[i], dir[i-1==0?6:i-1] + dir[i+1==7?1:i+1]);ll ans = 0;if(1ll * x * y <= 0)//目标在第二或第五象限或在x轴或y轴上 {if(x > 0) ans += 1ll * abs(x) * val[6];else ans += 1ll * abs(x) * val[3];if(y > 0) ans += 1ll * abs(y) * val[2];else ans += 1ll * abs(y) * val[5];}else//目标在第一或三、四、六象限或z轴上 {if(x > 0){int temp = min(abs(x), abs(y));ans += 1ll * temp * val[1];x -= temp;y -= temp;ans += 1ll * abs(x) * val[6];ans += 1ll * abs(y) * val[2];}else{int temp = min(abs(x), abs(y));ans += 1ll * temp * val[4];x += temp;y += temp;ans += 1ll * abs(x) * val[3];ans += 1ll * abs(y) * val[5];}}printf("%lld\n",ans);}return 0;
}

更多推荐

Codeforces Round #676 (Div. 2) D. Hexagons(思维)

本文发布于:2024-03-23 19:57:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1742200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:思维   Codeforces   Hexagons   Div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!