[CF1421D] Hexagons

[CF1421D] Hexagons

Description

给定一个由六边形密铺组成的坐标系中的点的坐标规律。从(0,0)出发,要到达(x,y),6种方向每种走一步都有一个费用,询问最小到达终点的费用。

Solution

如果让所有 a(x) 对 a(x-1) + a(x+1) 取 Min,那么最后一定能分解成不超过两段

枚举方向,对于一个确定的方向,解方程即可

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int dx[6] = {1, 0, -1, -1, 0, 1};
const int dy[6] = {1, 1, 0, -1, -1, 0};

void solve()
{
    int x, y, c[6];
    cin >> x >> y;
    for (int i = 0; i < 6; i++)
        cin >> c[i];
    for (int i = 0; i < 6; i++)
        c[(i + 1) % 6] = min(c[(i + 1) % 6], c[i] + c[(i + 2) % 6]);
    int ans = 2e18;
    for (int i = 0; i < 6; i++)
    {
        for (int j = i + 1; j < 6; j++)
        {
            if ((dx[j] * dy[i] - dx[i] * dy[j]) == 0)
                continue;
            if ((dx[i] * dy[j] - dx[j] * dy[i]) == 0)
                continue;
            int ai = (dx[j] * y - x * dy[j]) / (dx[j] * dy[i] - dx[i] * dy[j]);
            int aj = (dx[i] * y - x * dy[i]) / (dx[i] * dy[j] - dx[j] * dy[i]);
            if (ai >= 0 && aj >= 0)
                ans = min(ans, ai * c[i] + aj * c[j]);
        }
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
原文地址:https://www.cnblogs.com/mollnn/p/14488620.html