CF-Codeforces Round #676 (Div. 2)-D. Hexagons【思维】

it2025-02-17  26

题目链接 题意:给定一个六边形网格,往每一个方向走的花费是固定的,问从原点到 ( x , y ) (x,y) (x,y)的最小花费。 思路:可以发现一定可以只走两个方向走到终点,并且这样一定是最优的,我们通过观察发现,(为和二维坐标系对应,我们把x,y反过来说…),我们发现 c 1 , c 2 c_1,c_2 c1,c2方向对 x x x具有正贡献, c 4 , c 5 c_4,c_5 c4,c5方向对 x x x具有负贡献, c 1 , c 6 c_1,c_6 c1,c6方向对 y y y具有正贡献, c 3 , c 4 c_3,c_4 c3,c4方向对 y y y具有负贡献,同时注意一下 c 1 , c 4 c_1,c_4 c1,c4是对两个方向都有贡献的;根据以上条件,直接枚举这几种情况就行了。 具体思路看看代码:

#include <bits/stdc++.h> #define ll long long using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } /***************main****************/ ll T=1; ll m,n; ll calc(ll x, ll y, ll px, ll nx, ll py, ll ny){ ll ans = 0; ans += (x>0?px:nx) * abs(x); ans += (y>0?py:ny) * abs(y); return ans; } signed main() { T=read(); while (T--) { ll x,y;cin>>y>>x; ll c[7]; rep(i,1,7) cin>>c[i]; ll ans = calc(x,y,c[2],c[5],c[6],c[3]); ans=min(ans, calc(x-y, y, c[2], c[5], c[1], c[4])); ans=min(ans, calc(x, y-x, c[1], c[4], c[6], c[3])); cout<<ans<<endl; } return 0; }
最新回复(0)