【Codeforces 1421 D】Hexagons,贪心,模拟

problem

D. Hexagons
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

solution

/*
题意:
+ 给出一个坐标(x,y)和六个方向每步的代价
+ 求从原点按六边形模式走到终点的最小代价。
思路:
+ 从原点出发有6个方向,可是到这6个点的最小代价=min(直线,旁边两个V),所以先更新最小代价。
+ 然后直接模拟去走就行
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 998244353;
LL c[7];
int main(){
	int T;  cin>>T;
	while(T--){
		LL x, y;  cin>>x>>y;
		for(int i=1; i<=6; i++)cin>>c[i];
		c[1] = min(c[1],c[6]+c[2]);
		c[6] = min(c[6],c[5]+c[1]);
		for(int i=2; i<=5; i++)
			c[i] = min(c[i],c[i-1]+c[i+1]);
		if(x>=0){
			if(y>=x)cout<<c[1]*x+c[2]*(y-x)<<"\n";
			else if(y<=0)cout<<c[6]*x+c[5]*abs(y)<<"\n";
			else cout<<c[1]*y+c[6]*(x-y)<<"\n";
		}else{
			if(y<=x)cout<<c[4]*abs(x)+c[5]*(abs(y)-abs(x))<<"\n";
			else if(y>=0)cout<<c[3]*abs(x)+c[2]*y<<"\n";
			else cout<<c[4]*abs(y)+c[3]*(abs(x)-abs(y))<<"\n";
		}
	}
	return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页