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 𝑐1, 𝑐2, 𝑐3, 𝑐4, 𝑐5, 𝑐6 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 𝑡 (1≤𝑡≤104). Description of the test cases follows.
The first line of each test case contains two integers 𝑥 and 𝑦 (−109≤𝑥,𝑦≤109) representing the coordinates of the target hexagon.
The second line of each test case contains six integers 𝑐1, 𝑐2, 𝑐3, 𝑐4, 𝑐5, 𝑐6 (1≤𝑐1,𝑐2,𝑐3,𝑐4,𝑐5,𝑐6≤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
inputCopy
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
outputCopy
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking 𝑐3 3 times and 𝑐2 once, amounting to 5+5+5+3=18.
题意:
蜂窝形的地图,可以向周围6个方向走,每个方向走的花费不同,求从(0,0)走到(x,y)的最小花费。
思路:
感觉写的很麻烦。
将蜂窝形地图看作为直角坐标轴,则6个方向分别对应上,右上,右,下,左下,左。
先考虑上下左右的方向,那么左右肯定会合并成只有左或者只有右,上下会合并成只有上或只有下。
如果同时考虑右上和左下,那么很明显会合并成左下和右上一个。
考虑方向左,上,右上:
左,上一部分合并成左上,最后只剩下左或者上。
然后左上和右上一部分合并成上,剩下左上和右上一个。
这样结果就成了
- 左,上
- 左,右上
- 上,右上
其他部分合并也可以推出这个结论。
于是就成了,要么是
- 左,上
- 左,下
- 右,上
- 右,下
- 左下+上下左右中的一个
- 右上+上下左右中的一个
然后分类讨论一下即可。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 7;
const double eps = 1e-6;
int main() {
int T;scanf("%d",&T);
while(T--) {
ll x,y;scanf("%lld%lld",&x,&y);
ll c1,c2,c3,c4,c5,c6;
scanf("%lld%lld%lld%lld%lld%lld",&c1,&c2,&c3,&c4,&c5,&c6);
swap(x,y);
ll ans = 0;
if(x <= 0) {
ans += -c5 * x;
} else {
ans += c2 * x;
}
if(y <= 0) {
ans += -c3 * y;
} else {
ans += c6 * y;
}
//c1 c6
if(x >= 0) {
ll tmp = c1 * x;
ll cy = y - x;
if(cy >= 0) {
tmp += c6 * cy;
ans = min(ans,tmp);
}
}
//c1 c2
if(y >= 0) {
ll tmp = c1 * y;
ll cx = x - y;
if(cx >= 0) {
tmp += cx * c2;
ans = min(ans,tmp);
}
}
//c1 c5
if(y >= 0) {
ll tmp = c1 * y;
ll cx = x - y;
if(cx <= 0) {
tmp += -cx * c5;
ans = min(ans,tmp);
}
}
//c1 c3
if(x >= 0) {
ll tmp = c1 * x;
ll cy = y - x;
if(cy <= 0) {
tmp += -cy * c3;
ans = min(ans,tmp);
}
}
//c4 c2
if(y <= 0) {
ll tmp = -c4 * y;
ll cx = x - y;
if(cx >= 0) {
tmp += cx * c2;
ans = min(ans,tmp);
}
}
//c4 c3
if(x <= 0) {
ll tmp = -c4 * x;
ll cy = y - x;
if(cy <= 0) {
tmp += -cy * c3;
ans = min(ans,tmp);
}
}
//c4 c5
if(y <= 0) {
ll tmp = -c4 * y;
ll cx = x - y;
if(cx <= 0) {
tmp += -cx * c5;
ans = min(ans,tmp);
}
}
//c4 c6
if(x <= 0) {
ll tmp = -c4 * x;
ll cy = y - x;
if(cy >= 0) {
tmp += cy * c6;
ans = min(ans,tmp);
}
}
printf("%lld\n",ans);
}
return 0;
}