QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#10961. DFS Order

统计

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

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

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

Sample Data

Sample Input

4
1
2 3
40 6 11

Sample Output

10

Sample Input 2

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

Sample Output 2

5

Sample Input 3

4
-1
-2 300
4 -5 6

Sample Output 3

9

Sample Explanation

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

Constraints

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

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.