题目 › usaco-1476

DFS Order

USACO 2025 January · 铂金组 · 2025-01 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie has a simple undirected graph with vertices labeled $1\dots N$ ($2\le N\le 750$). She generates a depth-first search (DFS) order of the graph by calling the function $\texttt{dfs}(1)$, defined by the following C++ code. Each adjacency list ($\texttt{adj}[i]$ for all $1\le i\le N$) may be permuted arbitrarily before starting the depth first search, so a graph can have multiple possible DFS orders.

vector<bool> vis(N + 1);
vector<vector<int>> adj(N + 1);  // adjacency list
vector<int> dfs_order;

void dfs(int x) {
    if (vis[x]) return;
    vis[x] = true;
    dfs_order.push_back(x);
    for (int y : adj[x]) dfs(y);
}

You are given the initial state of the graph as well as the cost to change the state of each edge. Specifically, for every pair of vertices $(i,j)$ satisfying $1\le i<j\le N$, you are given an integer $a_{i,j}$ ($0<|a_{i,j}|\le 1000$) such that

  • If $a_{i,j}>0$, edge $(i,j)$ is not currently in the graph, and can be added for cost $a_{i,j}$.
  • If $a_{i,j}<0$, edge $(i,j)$ is currently in the graph, and can be removed for cost $-a_{i,j}$.

Determine the minimum total cost to change the graph so that $[1,2\dots,N]$ is a possible DFS ordering.

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

The first line contains $N$.

Then $N-1$ lines follow. The $j-1$th line contains $a_{1,j}, a_{2,j}, \dots, a_{j-1,j}$ separated by spaces.

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

The minimum cost to change the graph so that $[1,2,\dots, N]$ is a possible DFS ordering.

SAMPLE INPUT:

4
1
2 3
40 6 11

SAMPLE OUTPUT:

10

Initially, the graph contains no edges. $(1,2),(2,3),(2,4)$ can be added for a total cost of $1+3+6$. The graph now has two possible DFS orderings: $[1,2,3,4],[1,2,4,3]$.

SAMPLE INPUT:

5
-1
10 -2
10 -7 10
-6 -4 -5 10

SAMPLE OUTPUT:

5

Initially, the graph contains edges $(1,2),(2,3),(2,4),(1,5),(2,5),(3,5)$. Edge $(3,5)$ can be removed for a cost of $5$.

SAMPLE INPUT:

4
-1
-2 300
4 -5 6

SAMPLE OUTPUT:

9

Initially, the graph contains edges $(1,2),(1,3),(2,4)$. Edge $(2,4)$ can be removed and edge $(1,4)$ can be added for a total cost of $5+4=9$.

SCORING:

  • Inputs 4-9: All $a_{i,j}>0$
  • Inputs 10-16: $N\le 50$
  • Inputs 17-23: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

using int64 = long long;
const int64 INF = (int64)4e18;

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

    int N;
    cin >> N;

    vector<vector<int>> add(N + 1, vector<int>(N + 1, 0));
    vector<vector<int>> saving(N + 1, vector<int>(N + 1, 0));

    int64 removeAll = 0;
    for (int j = 2; j <= N; ++j) {
        for (int i = 1; i < j; ++i) {
            int a;
            cin >> a;
            if (a > 0) {
                add[i][j] = a;
            } else {
                saving[i][j] = -a;
                removeAll += -a;
            }
        }
    }

    vector<vector<int64>> dp(N + 1, vector<int64>(N + 1, 0));
    vector<int64> forest(N + 1, 0);

    for (int i = N; i >= 1; --i) {
        dp[i][i] = 0;
        forest[i] = 0;
        int64 saved = 0;

        for (int r = i + 1; r <= N; ++r) {
            saved += saving[i][r];
            int64 best = INF;

            for (int before = i; before < r; ++before) {
                int child = before + 1;
                int64 candidate = forest[before] + dp[child][r]
                                + add[i][child];
                best = min(best, candidate);
            }

            forest[r] = best;
            dp[i][r] = best - saved;
        }
    }

    cout << removeAll + dp[1][N] << '\n';
    return 0;
}