题面
来源: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
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
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
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 14.7k平均输出 token(含思考) ↓
- 200.7输出速度 tokens/s ↑
- 65 秒延迟 ↓
- 0.60×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#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; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> a(N + 2, vector<int>(N + 2, 0)); for (int j = 2; j <= N; ++j) { for (int i = 1; i < j; ++i) { cin >> a[i][j]; } } // inside[l][r]: total cost of all initially present edges // whose two endpoints lie in [l,r]. vector<vector<long long>> inside( N + 2, vector<long long>(N + 2, 0) ); for (int l = N; l >= 1; --l) { long long row_sum = 0; for (int r = l; r <= N; ++r) { if (r > l && a[l][r] < 0) { row_sum += -a[l][r]; } inside[l][r] = inside[l + 1][r] + row_sum; } } // dp[l][r]: minimum cost for [l,r] to be the subtree rooted at l, // considering only edges with both endpoints in this interval. vector<vector<long long>> dp( N + 2, vector<long long>(N + 2, 0) ); vector<long long> best(N + 2, 0); for (int l = N; l >= 1; --l) { best[l] = 0; for (int r = l + 1; r <= N; ++r) { long long cur = LLONG_MAX / 4; // Partition [l+1,r] into the child subtrees of l. // The last child subtree is [s,r]. for (int s = l + 1; s <= r; ++s) { long long add_tree_edge = max(a[l][s], 0); long long candidate = best[s - 1] + dp[s][r] - inside[s][r] + add_tree_edge; cur = min(cur, candidate); } best[r] = cur; dp[l][r] = inside[l + 1][r] + cur; } } cout << dp[1][N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> add(N + 2, vector<int>(N + 2, 0)); vector<vector<int>> rem(N + 2, vector<int>(N + 2, 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 rem[i][j] = -a; } } // cross[l][r]: cost of removing initial edges from l to vertices after r. vector<vector<long long>> cross(N + 2, vector<long long>(N + 2, 0)); for (int l = 1; l <= N; ++l) { for (int r = N - 1; r >= l; --r) { cross[l][r] = cross[l][r + 1] + rem[l][r + 1]; } } // dp[l][r]: minimum cost for a subtree rooted at l whose preorder // interval is exactly [l,r]. vector<vector<long long>> dp(N + 2, vector<long long>(N + 2, 0)); const long long INF = (1LL << 62); for (int l = N; l >= 1; --l) { // forest[x]: minimum cost of the children of l covering [l+1,x]. vector<long long> forest(N + 2, INF); forest[l] = 0; dp[l][l] = cross[l][l]; for (int r = l + 1; r <= N; ++r) { long long best = INF; for (int c = l + 1; c <= r; ++c) { long long candidate = forest[c - 1] + add[l][c] + dp[c][r]; best = min(best, candidate); } forest[r] = best; dp[l][r] = cross[l][r] + best; } } cout << dp[1][N] << '\n'; return 0; }
#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; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> a(N + 2, vector<int>(N + 2, 0)); for (int j = 2; j <= N; ++j) { for (int i = 1; i < j; ++i) { cin >> a[i][j]; } } // inside[l][r]: total cost of all initially present edges // whose two endpoints lie in [l,r]. vector<vector<long long>> inside( N + 2, vector<long long>(N + 2, 0) ); for (int l = N; l >= 1; --l) { long long row_sum = 0; for (int r = l; r <= N; ++r) { if (r > l && a[l][r] < 0) { row_sum += -a[l][r]; } inside[l][r] = inside[l + 1][r] + row_sum; } } // dp[l][r]: minimum cost for [l,r] to be the subtree rooted at l, // considering only edges with both endpoints in this interval. vector<vector<long long>> dp( N + 2, vector<long long>(N + 2, 0) ); vector<long long> best(N + 2, 0); for (int l = N; l >= 1; --l) { best[l] = 0; for (int r = l + 1; r <= N; ++r) { long long cur = LLONG_MAX / 4; // Partition [l+1,r] into the child subtrees of l. // The last child subtree is [s,r]. for (int s = l + 1; s <= r; ++s) { long long add_tree_edge = max(a[l][s], 0); long long candidate = best[s - 1] + dp[s][r] - inside[s][r] + add_tree_edge; cur = min(cur, candidate); } best[r] = cur; dp[l][r] = inside[l + 1][r] + cur; } } cout << dp[1][N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> add(N + 2, vector<int>(N + 2, 0)); vector<vector<int>> rem(N + 2, vector<int>(N + 2, 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 rem[i][j] = -a; } } // cross[l][r]: cost of removing initial edges from l to vertices after r. vector<vector<long long>> cross(N + 2, vector<long long>(N + 2, 0)); for (int l = 1; l <= N; ++l) { for (int r = N - 1; r >= l; --r) { cross[l][r] = cross[l][r + 1] + rem[l][r + 1]; } } // dp[l][r]: minimum cost for a subtree rooted at l whose preorder // interval is exactly [l,r]. vector<vector<long long>> dp(N + 2, vector<long long>(N + 2, 0)); const long long INF = (1LL << 62); for (int l = N; l >= 1; --l) { // forest[x]: minimum cost of the children of l covering [l+1,x]. vector<long long> forest(N + 2, INF); forest[l] = 0; dp[l][l] = cross[l][l]; for (int r = l + 1; r <= N; ++r) { long long best = INF; for (int c = l + 1; c <= r; ++c) { long long candidate = forest[c - 1] + add[l][c] + dp[c][r]; best = min(best, candidate); } forest[r] = best; dp[l][r] = cross[l][r] + best; } } cout << dp[1][N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const int MAXN = 750; long long w[MAXN][MAXN]; long long dp[MAXN][MAXN]; long long g[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; long long base = 0; for (int j = 1; j < N; ++j) { for (int i = 0; i < j; ++i) { cin >> w[i][j]; if (w[i][j] < 0) base += -w[i][j]; } } const long long INF = (1LL << 60); for (int l = N - 1; l >= 0; --l) { for (int i = 0; i < N; ++i) g[i] = INF; g[l] = 0; long long negSum = 0; for (int r = l + 1; r < N; ++r) { negSum += min(0LL, w[l][r]); long long best = INF; for (int c = l + 1; c <= r; ++c) { long long positiveEdgeCost = max(0LL, w[l][c]); long long cur = g[c - 1] + dp[c][r] + positiveEdgeCost; if (cur < best) best = cur; } g[r] = best; dp[l][r] = best + negSum; } } cout << base + dp[0][N - 1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 755; const long long INF = 4000000000000000000LL; int addCost[MAXN][MAXN]; int remCost[MAXN][MAXN]; int rowPref[MAXN][MAXN]; long long dp[MAXN][MAXN]; long long coverArr[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; for (int j = 2; j <= N; j++) { for (int i = 1; i < j; i++) { int a; cin >> a; if (a > 0) { addCost[i][j] = a; } else { int c = -a; remCost[i][j] = remCost[j][i] = c; } } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { rowPref[i][j] = rowPref[i][j - 1] + remCost[i][j]; } } for (int l = N; l >= 1; l--) { fill(coverArr, coverArr + N + 2, INF); coverArr[l] = 0; int L = l + 1; int Lm1 = L - 1; for (int pos = l; pos <= N - 1; pos++) { if (coverArr[pos] >= INF / 2) continue; int s = pos + 1; long long base = coverArr[pos] + addCost[l][s]; long long crossCost = 0; for (int t = s; t <= N; t++) { crossCost += rowPref[t][pos] - rowPref[t][Lm1]; long long cand = base + dp[s][t] + crossCost; if (cand < coverArr[t]) { coverArr[t] = cand; } } } for (int r = l; r <= N; r++) { dp[l][r] = coverArr[r]; } } cout << dp[1][N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static int a[755][755]; static long long dp[755][755]; static long long part[755]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; for (int j = 2; j <= N; ++j) { for (int i = 1; i < j; ++i) { cin >> a[i][j]; } } const long long INF = (1LL << 60); long long base = 0; for (int i = 1; i <= N; ++i) { for (int j = i + 1; j <= N; ++j) { if (a[i][j] < 0) base += -a[i][j]; } } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { dp[i][j] = INF; } dp[i][i] = 0; } for (int p = N; p >= 1; --p) { part[p] = 0; long long prefNeg = 0; for (int pos = p + 1; pos <= N; ++pos) { prefNeg += min(0, a[p][pos]); long long best = INF; for (int k = p + 1; k <= pos; ++k) { long long addCost = max(0, a[p][k]); long long val = part[k - 1] + dp[k][pos] + addCost; if (val < best) best = val; } part[pos] = best; dp[p][pos] = prefNeg + best; } } cout << base + dp[1][N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; if (!(cin >> N)) return 0; const int W = N + 1; const size_t SZ = (size_t)W * W; vector<int> addc(SZ, 0); // cost to add edge (i,j) if currently missing vector<int> remc(SZ, 0); // cost to remove edge (i,j) if currently present for (int j = 2; j <= N; ++j) { for (int i = 1; i < j; ++i) { int a; cin >> a; addc[(size_t)i * W + j] = (a > 0 ? a : 0); remc[(size_t)i * W + j] = (a < 0 ? -a : 0); } } // pref[x][y] = sum_{u<=x, v<=y} remc[u][v] (2-D prefix sums) vector<long long> pref(SZ, 0); for (int x = 1; x <= N; ++x) { long long rs = 0; const size_t row = (size_t)x * W, prow = (size_t)(x - 1) * W; for (int y = 1; y <= N; ++y) { rs += remc[row + y]; pref[row + y] = pref[prow + y] + rs; } } vector<long long> dp(SZ, 0); vector<long long> Y(W, 0); // Y[s] = dp[s][j] + pref[s-1][j] - pref[s-1][s-1] for (int j = 1; j <= N; ++j) { dp[(size_t)j * W + j] = 0; Y[j] = pref[(size_t)(j - 1) * W + j] - pref[(size_t)(j - 1) * W + (j - 1)]; for (int i = j - 1; i >= 1; --i) { const size_t off = (size_t)i * W; long long best = LLONG_MAX; for (int s = i + 1; s <= j; ++s) { long long cand = dp[off + s - 1] + (long long)addc[off + s] + pref[off + s - 1] + Y[s]; if (cand < best) best = cand; } long long val = best - pref[off + j]; dp[off + j] = val; Y[i] = val + pref[(size_t)(i - 1) * W + j] - pref[(size_t)(i - 1) * W + (i - 1)]; } } cout << dp[(size_t)1 * W + N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; if (!(cin >> N)) return 0; // A[i][j] for i<j : positive => edge absent (add cost), negative => present (remove cost) vector<vector<int>> A(N + 2, vector<int>(N + 2, 0)); for (int j = 2; j <= N; j++) for (int i = 1; i < j; i++) cin >> A[i][j]; // E[i][j] = cost to make tree edge (i,j) present vector<vector<int>> E(N + 2, vector<int>(N + 2, 0)); for (int i = 1; i <= N; i++) for (int j = i + 1; j <= N; j++) E[i][j] = max(A[i][j], 0); // D[i][j] = total removal cost of present edges (i,k) with k > j vector<vector<long long>> D(N + 2, vector<long long>(N + 2, 0)); for (int i = 1; i <= N; i++) { for (int j = N; j >= i; j--) { D[i][j] = D[i][j + 1]; if (A[i][j + 1] < 0) D[i][j] += -(long long)A[i][j + 1]; } } const long long INF = (1LL << 60); // dpT[r][l] = dp[l][r]: min cost of a subtree rooted at l spanning [l..r] vector<vector<long long>> dpT(N + 2, vector<long long>(N + 2, 0)); vector<long long> f(N + 2, 0); for (int l = N; l >= 1; l--) { f[l] = 0; for (int r = l + 1; r <= N; r++) { long long best = INF; const long long *drow = dpT[r].data(); const int *erow = E[l].data(); for (int m = l + 1; m <= r; m++) { long long cand = f[m - 1] + drow[m] + (long long)erow[m]; if (cand < best) best = cand; } f[r] = best; } for (int r = l; r <= N; r++) dpT[r][l] = D[l][r] + f[r]; } cout << dpT[N][1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const int MAXN = 760; static int wgt[MAXN][MAXN]; // wgt[i][j] = cost of deleting existing edge (i,j) static int addc[MAXN][MAXN]; // addc[i][j] = cost of adding missing edge (i,j) static int Pt[MAXN][MAXN]; // Pt[t][x] = P_x(t) = sum_{y=x+1..t} wgt[x][y] static long long Ht[MAXN][MAXN]; // Ht[t][i] = H[i][t] static long long row[MAXN]; // current DP row H[i][*] int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; if (!(cin >> N)) return 0; for (int j = 2; j <= N; j++) { for (int i = 1; i < j; i++) { int a; cin >> a; if (a < 0) wgt[i][j] = -a; // existing edge else addc[i][j] = a; // missing edge } } // Prefix sums of w, stored transposed: Pt[t][x] = sum_{y=x+1..t} wgt[x][y] for (int x = 1; x <= N; x++) { int run = 0; for (int t = x + 1; t <= N; t++) { run += wgt[x][t]; Pt[t][x] = run; } } // Baseline: every pair (x,y) with 2 <= x < y would cost w(x,y) unless x is an ancestor of y long long base = 0; for (int x = 2; x <= N; x++) base += Pt[N][x]; const long long INF = (1LL << 62); for (int i = N; i >= 1; i--) { row[i] = 0; // H[i][i] = 0 for (int pos = i + 1; pos <= N; pos++) { long long best = INF; const long long* hrow = Ht[pos]; // H[c][pos] const int* prow = Pt[pos]; // P_c(pos) for (int c = i + 1; c <= pos; c++) { long long v = row[c - 1] + hrow[c] + (long long)addc[i][c] - prow[c]; if (v < best) best = v; } row[pos] = best; Ht[pos][i] = best; } } cout << base + Ht[N][1] << '\n'; return 0; }