题目 › usaco-1570

Lexicographically Smallest Path

USACO 2026 Second · 金组 · 2026-01 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie is given an undirected graph with $N$ ($1\le N\le 2 \cdot 10^5$) vertices labeled $1\dots N$ and $M$ edges ($N - 1\le M\le 2 \cdot 10^5$). Each edge is described by two integers $u, v$ ($1\le u, v \le N$) describing an undirected edge between nodes $u$ and $v$ and a lowercase Latin letter $c$ in the range a..z that is the value on the edge. The graph given is guaranteed to be connected. There may be multiple edges or self-loops.

Define $f(a, b)$ as the lexicographically smallest concatenation of edge values over all paths starting from node $a$ and ending at node $b$. A path may contain the same edge more than once (i.e., cycles are allowed).

For each $i$ ($1\le i \le N$), help Bessie determine the length of $f(1, i)$. Output this length if it is finite, otherwise output $-1$.

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

The first line contains $T$ ($1\le T\le 10$), the number of independent tests. Each test is specified in the following format:

The first line contains $N$ and $M$.

The next $M$ lines each contain two integers followed by a lowercase Latin character.

It is guaranteed that neither the sum of $N$ nor the sum of $M$ over all tests exceeds $4\cdot 10^5$.

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

For each test, output $N$ space-separated integers on a new line.

SAMPLE INPUT:

2
1 0
2 2
1 1 a
2 1 b

SAMPLE OUTPUT:

0
0 -1

For the first test case, node 1 can be reached with an empty path, so the answer is 0. In the second test case, node 2 cannot have a lexicographically smallest path, since FJ can repeat the 'a' self-loop any number of times before moving to node 2, producing arbitrarily long strings that are still lexicographically minimal. Therefore, the answer for node 2 is -1.

SAMPLE INPUT:

2
7 7
1 2 a
1 3 a
2 4 b
3 5 a
5 6 a
6 7 a
7 4 a
4 3
1 2 z
2 3 x
3 4 y

SAMPLE OUTPUT:

0 1 1 5 2 3 4
0 1 2 -1
For the first test case, node 1 has distance 0. Nodes 2 and 3 are adjacent with node 1, and they have distance 1. For nodes 4, 5, 6, and 7, it can be proven that the lexicographically shortest path does not pass through the edge between node 2 and 4.

For the second test case, node 4 again has no lexicographically smallest path, since the string can be extended indefinitely while remaining lexicographically minimal. Thus, its answer is -1.

SCORING:

  • Inputs 3-4: Every character is a.
  • Inputs 5-8: Every character is a or b.
  • Inputs 9-14: $N,M\le 5000$
  • Inputs 15-22: No additional constraints.

Problem credits: Daniel Zhu and Yash Belani

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 9.0k平均输出 token(含思考) ↓
  • 20.5输出速度 tokens/s ↑
  • 214 秒延迟 ↓
  • 0.50×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 64.5k平均输出 token(含思考) ↓
  • 182.0输出速度 tokens/s ↑
  • 318 秒延迟 ↓
  • 0.53×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 52.8k平均输出 token(含思考) ↓
  • 62.3输出速度 tokens/s ↑
  • 810 秒延迟 ↓
  • 0.42×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

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

代码

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

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

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

        vector<vector<pair<int, int>>> adj(N + 1);
        for (int i = 0; i < M; i++) {
            int u, v;
            char c;
            cin >> u >> v >> c;
            int w = c - 'a';
            adj[u].push_back({v, w});
            adj[v].push_back({u, w});
        }

        vector<int> dist(N + 1, -1);
        dist[1] = 0;
        vector<int> cur = {1};

        bool hasInfinite = false;
        int infiniteChar = -1;
        bool stopped = false;

        while (!cur.empty() && !stopped) {
            for (int c = 0; c < 26; c++) {
                vector<int> nxt;
                bool cycle = false;

                for (int u : cur) {
                    for (auto [v, w] : adj[u]) {
                        if (w != c) continue;
                        if (dist[v] == -1) {
                            dist[v] = dist[cur[0]] + 1;
                            nxt.push_back(v);
                        } else {
                            cycle = true;
                        }
                    }
                }

                if (!nxt.empty()) {
                    hasInfinite = cycle || (hasInfinite && infiniteChar == c);
                    infiniteChar = hasInfinite ? c : -1;
                    cur.swap(nxt);
                    break;
                }

                if (cycle || (hasInfinite && infiniteChar == c)) {
                    stopped = true;
                    break;
                }

                if (c == 25) cur.clear();
            }
        }

        for (int i = 1; i <= N; i++) {
            if (i > 1) cout << ' ';
            cout << dist[i];
        }
        cout << '\n';
    }

    return 0;
}