DFS Order

시간 제한2초메모리 제한2048 MB

요약
주어진 비용으로 무방향 그래프의 간선을 바꾸어 1,2,...,N이 꼭짓점 1의 DFS 순서가 될 수 있게 할 때 최소 비용을 구한다.
난이도

어려움10점 중 8점

유형
DFS, 그래프, 동적 계획법, 그리디
정답자
아직 제출이 없습니다

문제

Bessie has a simple undirected graph with vertices labeled 1…N1\dots N (2≤N≤7502\le N\le 750). She generates a depth-first search (DFS) order of the graph by calling the function dfs(11), defined by the following C++ code. Each adjacency list (adj[ii] for all 1≤i≤N1\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)(i,j) satisfying 1≤i\<j≤N1\le i\<j\le N, you are given an integer a_i,ja\_{i,j} (0<∣a_i,j∣≤10000<|a\_{i,j}|\le 1000) such that

  • If a_i,j>0a\_{i,j}>0, edge (i,j)(i,j) is not currently in the graph, and can be added for cost a_i,ja\_{i,j}.
  • If a_i,j<0a\_{i,j}<0, edge (i,j)(i,j) is currently in the graph, and can be removed for cost −a_i,j-a\_{i,j}.

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

입력

The first line contains NN.

Then N−1N-1 lines follow. The j−1j-1th line contains a_1,j,a_2,j,…,a_j−1,ja\_{1,j}, a\_{2,j}, \dots, a\_{j-1,j} separated by spaces.

출력

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

예제3

  1. 예제 1

    입력
    4
    1
    2 3
    40 6 11
    
    예상 출력
    10
    
  2. 예제 2

    입력
    5
    -1
    10 -2
    10 -7 10
    -6 -4 -5 10
    
    예상 출력
    5
    
  3. 예제 3

    입력
    4
    -1
    -2 300
    4 -5 6
    
    예상 출력
    9