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
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 c3 3 times and c2 once, amounting to 5+5+5+3=18.
***思路:***一开始我看到这个题目有点蒙,但是后来我发现,我们只要把这个六边形转换成坐标系就看起来方便多了。
然后对每一个分块可能的点分开进行讨论就好了。(注意六边形变成坐标系x和y要互换)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll c[10];
int main()
{
int t;
cin>>t;
while(t--)
{
int x,y;
cin>>x>>y;
swap(x,y);
for(int i=1;i<=6;i++){
cin>>c[i];
}
ll ans=0;
if(x>0&&y>0)
{
if(x>=y)
{
ans=min(min(c[2]*x+c[6]*y,c[2]*(x-y)+c[1]*y),c[1]*x+c[3]*(x-y));
}else
{
ans=min(min(c[2]*x+c[6]*y,c[6]*(y-x)+c[1]*x),c[1]*y+c[5]*(y-x));
}
}else if(x>0&&y<0)
{
ans=min(min(c[2]*x+c[3]*(-y),c[3]*(x-y)+c[1]*x),c[2]*(x-y)+c[4]*(-y));
}else if(x<0&&y<0)
{
if(x>=y)
{
ans=min(min(c[3]*(-y)+c[5]*(-x),c[3]*(x-y)+c[4]*(-x)),c[4]*(-y)+c[2]*(x-y));
}else if(x<y)
{
ans=min(min(c[3]*(-y)+c[5]*(-x),c[4]*(-x)+c[6]*(y-x)),c[5]*(y-x)+c[4]*(-y));
}
}else if(x<0&&y>0)
{
ans=min(min(c[5]*(-x)+c[6]*y,c[5]*(y-x)+c[1]*y),c[6]*(y-x)+c[4]*(-x));
}else if(y==0&&x>0)
{
ans=min(c[2]*x,c[3]*x+c[1]*x);
}else if(x==0&&y<0){
int t=-y;
ans=min(c[3]*t,c[4]*t+c[2]*t);
}else if(y==0&&x<0)
{
int t=-x;
ans=min(c[5]*t,c[4]*t+c[6]*t);
}else if(x==0&&y>0)
{
int t=y;
ans=min(c[6]*t,c[1]*t+c[5]*t);
}
printf("%lld\n",ans);
}
return 0;
}