题目 › usaco-1596

All Pairs Shortest Paths

USACO 2026 Third · 铂金组 · 2026-02 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字,不含图片。

You have a bunch of triangular regions that tessellate an infinite 2D plane. The tesselation is defined as follows (see the diagram for a better understanding):

  • Recall that Euler's formula states that $e^{ix}=\cos (x)+i\sin (x)$ for real $x$. First, draw a vertex at $x+y\exp(\pi i/3)$ on the complex plane for all integers $x,y$.
  • Then for every three vertices from the step above forming an equilateral triangle with side length 1, draw the edges forming its border. Additionally, draw a vertex at the center of the triangle and edges from the center of the triangle to each of three outer vertices.

You are given $N$ ($2\le N\le 2\cdot 10^5$) input points, each of which lies strictly within some region (i.e., not on any vertex or edge). For any pair of the input points, define the distance between them to be the smallest number of edges crossed when drawing a path from one point to the other that doesn't pass through any vertex.

Output the sum of all $N(N-1)/2$ pairwise distances between the input points.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $T$ ($T\ge 1$), the number of independent tests. Each test is specified as follows:

The first line contains $N$.

The next $N$ lines each contain three integers $x$, $y$, and $z$ ($0\le x,y<10^6, 0\le z<12$) representing a point at $x+y\exp(\pi i/3)+ \epsilon\cdot \exp((1+2z)\pi i/12)$ on the complex plane ($\epsilon$ being a small positive number).

It is guaranteed that the sum of $N$ over all tests doesn't exceed $2\cdot 10^5$.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test, output the sum of all $N(N-1)/2$ pairwise distances on a new line.

SAMPLE INPUT:

6
2
0 0 0
0 0 0
2
0 0 0
1 1 7
2
0 0 0
0 0 6
3
0 0 1
0 0 5
0 0 9
2
0 2 11
1 1 1
2
2 0 11
1 1 1

SAMPLE OUTPUT:

0
3
6
12
2
6

The second test is illustrated below:

  • The vertex at $x+y\exp(\pi i/3)$ is labeled with $(x,y)$ for each $x\in[-1,2], y\in[-1,2]$.
  • Dots are drawn at the vertices mentioned above as well as the vertices that are the centers of each equilateral triangle.
  • The triangular region containing $(x,y,z)=(0,0,0)$ is highlighted in green.
  • The triangular region containing $(x,y,z)=(1,1,7)$ is highlighted in blue. Note that $15\pi/12=225^{\circ}$.
  • An example path from the first region to the second crossing three edges is drawn.

figure

SCORING:

  • Inputs 2-5: $N\le 10$, $0\le x,y<5$
  • Inputs 6-13: $N\le 10$
  • Inputs 14-21: $T=1$

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 28.6采样 3 · 答案错误
  • 52.0k平均输出 token(含思考) ↓
  • 29.8输出速度 tokens/s ↑
  • 2067 秒延迟 ↓
  • 0.31×CPU 时间 / 官方解 ↓
  • 更优 1、同阶 1时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

1/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 28.6采样 2 · 答案错误
  • 61.9采样 3 · 答案错误
  • 56.3k平均输出 token(含思考) ↓
  • 25.3输出速度 tokens/s ↑
  • 2406 秒延迟 ↓
  • 0.23×CPU 时间 / 官方解 ↓
  • 更优 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 0.0采样 3 · 答案错误
  • 92.9k平均输出 token(含思考) ↓
  • 169.1输出速度 tokens/s ↑
  • 588 秒延迟 ↓
  • 0.27×CPU 时间 / 官方解 ↓
  • 更优 2时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

1/3

解出的采样 ↑

  • 0.0采样 1 · 编译错误
  • 14.3采样 2 · 答案错误
  • 100采样 3 · 解出
  • 73.2k平均输出 token(含思考) ↓
  • 78.4输出速度 tokens/s ↑
  • 974 秒延迟 ↓
  • 0.23×CPU 时间 / 官方解 ↓
  • 更优 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 174 行
#include <bits/stdc++.h>
using namespace std;

using int64 = long long;

struct Node {
    array<int, 3> c;
    int port;
    int type; // 0 = A, 1 = B
    int same = 1;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    static const int offset[12][3] = {
        {0, 0, 0}, {0, 0, 0},
        {0, 0, 1}, {0, 0, 1},
        {-1, 0, 1}, {-1, 0, 1},
        {-1, 1, 1}, {-1, 1, 1},
        {-1, 1, 0}, {-1, 1, 0},
        {0, 1, 0}, {0, 1, 0}
    };
    static const int port[12] = {
        1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1
    };

    int T;
    cin >> T;

    while (T--) {
        int N;
        cin >> N;

        vector<Node> nodes(N);
        array<vector<int>, 3> values;

        for (int d = 0; d < 3; ++d) {
            values[d].reserve(N);
        }

        for (int i = 0; i < N; ++i) {
            int x, y, z;
            cin >> x >> y >> z;

            array<int, 3> translation = {x + y, -y, -x};
            for (int d = 0; d < 3; ++d) {
                nodes[i].c[d] = translation[d] + offset[z][d];
                values[d].push_back(nodes[i].c[d]);
            }

            nodes[i].port = port[z];
            nodes[i].type = ((z / 2) % 2); // A, A, B, B, ...
        }

        for (int d = 0; d < 3; ++d) {
            sort(values[d].begin(), values[d].end());
        }

        // Sum of L1 distances over all unordered pairs.
        int64 l1Sum = 0;
        for (int d = 0; d < 3; ++d) {
            int64 prefix = 0;
            for (int i = 0; i < N; ++i) {
                l1Sum += int64(i) * values[d][i] - prefix;
                prefix += values[d][i];
            }
        }

        // Group points having the same base coordinate.
        vector<int> order(N);
        iota(order.begin(), order.end(), 0);
        sort(order.begin(), order.end(), [&](int a, int b) {
            return nodes[a].c < nodes[b].c;
        });

        int64 sameBasePairs = 0;
        int64 sameBaseDistance = 0;

        for (int l = 0; l < N; ) {
            int r = l + 1;
            while (r < N && nodes[order[l]].c == nodes[order[r]].c) {
                ++r;
            }

            int sz = r - l;
            int64 cnt[3] = {0, 0, 0};

            for (int i = l; i < r; ++i) {
                int id = order[i];
                nodes[id].same = sz;
                ++cnt[nodes[id].port];
            }

            sameBasePairs += int64(sz) * (sz - 1) / 2;
            sameBaseDistance += cnt[0] * cnt[1] + cnt[0] * cnt[2]
                              + cnt[1] * cnt[2];
            l = r;
        }

        // Sum the independently determined endpoint penalties.
        int64 penalties = 0;
        for (const Node &node : nodes) {
            int d = node.port;
            int coordinate = node.c[d];

            int64 favorable;
            if (node.type == 0) {
                // A prefers the coordinate of the other endpoint to be larger.
                favorable = values[d].end()
                          - upper_bound(values[d].begin(), values[d].end(), coordinate);
            } else {
                // B prefers it to be smaller.
                favorable = lower_bound(values[d].begin(), values[d].end(), coordinate)
                          - values[d].begin();
            }

            penalties += (N - node.same) - favorable;
        }

        // Correction for opposite-type pairs whose two endpoint penalties
        // cannot both be avoided.
        int64 correction = 0;
        for (int d = 0; d < 3; ++d) {
            vector<int> bValues;
            vector<array<int, 3>> bCoordinates;

            for (const Node &node : nodes) {
                if (node.type == 1 && node.port == d) {
                    bValues.push_back(node.c[d]);
                    bCoordinates.push_back(node.c);
                }
            }

            sort(bValues.begin(), bValues.end());
            sort(bCoordinates.begin(), bCoordinates.end());

            for (const Node &node : nodes) {
                if (node.type != 0 || node.port != d) {
                    continue;
                }

                int wanted = node.c[d] + 1;
                auto rangeValues = equal_range(
                    bValues.begin(), bValues.end(), wanted
                );
                int64 candidates = rangeValues.second - rangeValues.first;

                array<int, 3> direct = node.c;
                ++direct[d];
                auto rangeDirect = equal_range(
                    bCoordinates.begin(), bCoordinates.end(), direct
                );
                int64 directNeighbors = rangeDirect.second - rangeDirect.first;

                correction += candidates - directNeighbors;
            }
        }

        int64 totalPairs = int64(N) * (N - 1) / 2;
        int64 distinctBasePairs = totalPairs - sameBasePairs;

        int64 answer = 2 * l1Sum
                     - distinctBasePairs
                     + sameBaseDistance
                     + penalties
                     + correction;

        cout << answer << '\n';
    }

    return 0;
}