Logo
(追記) (追記ここまで)

33498번 - DFS Order 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB19141473.684%

문제

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 dfs(1ドル$), defined by the following C++ code. Each adjacency list (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.

입력

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.

출력

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

제한

예제 입력 1

4
1
2 3
40 6 11

예제 출력 1

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]$.

예제 입력 2

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

예제 출력 2

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ドル$.

예제 입력 3

4
-1
-2 300
4 -5 6

예제 출력 3

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$.

힌트

출처

Olympiad > USA Computing Olympiad > 2024-2025 Season > USACO 2025 January Contest > Platinum 1번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /