One-Way Abyss

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

요약
수직 갱도 사이의 가로 터널이 위에서 아래 순서로 주어지고 만나면 반드시 진입해야 할 때, 보물 합을 최대로 만드는 시작 갱도를 찾는다.
난이도

보통10점 중 7점

유형
동적 계획법, 그래프, 구현, 정렬
정답자
아직 제출이 없습니다

문제

Mitty is a brave adventurer exploring a mysterious underground cave system known as The Abyss. The Abyss is composed of nn parallel vertical shafts and mm horizontal tunnels. Each tunnel connects exactly two shafts at the same depth. All mm tunnels have distinct depths, and surprisingly, there is a treasure in the middle of every tunnel!

Mitty can pick any shaft to start with. He moves downward from the top of the chosen shaft, obeying the following rules:

  • He can only move downward, going upward is not allowed.
  • Whenever he encounters a horizontal tunnel, he must enter it immediately and will arrive at the connected shaft.
  • Once he enters a horizontal tunnel, he cannot go back.

These treasures in the tunnels have various values. Mitty wants to collect as much treasure as possible. Please help him calculate the maximum total value of treasures he can collect when starting from one of the shafts.

입력

Each test contains multiple test cases. The first line contains the number of test cases tt. The description of the test cases follows.

The first line contains two integers nn and mm, representing the number of vertical shafts and horizontal tunnels, respectively.

Each of the following mm lines contains three integers xx, yy and vv, representing a horizontal tunnel at a certain depth that connects shafts numbered xx and yy, and contains a treasure worth vv.

The horizontal tunnels are given from top to bottom. This implies that no two horizontal tunnels situated at the same depth.

출력

For each test case, print a single integer, representing the maximum total value of treasures Mitty can collect.

제한

  • 1≤t≤201 ≤ t ≤ 20
  • 1≤n≤2×1051 ≤ n ≤ 2 \times 10^5
  • 0≤m≤2×1050 ≤ m ≤ 2 \times 10^5
  • 1≤x<y≤n1 ≤ x < y ≤ n
  • 0≤v≤1090 ≤ v ≤ 10^9
  • It is guaranteed that the sum of nn over all test cases does not exceed 2×1052 \times 10^5.
  • It is guaranteed that the sum of mm over all test cases does not exceed 2×1052 \times 10^5.

예제2

  1. 예제 1

    입력
    1
    3 3
    1 2 3
    2 3 4
    1 3 9
    
    예상 출력
    16
    
  2. 예제 2

    입력
    2
    5 8
    1 4 5
    1 3 4
    1 3 2
    1 3 9
    2 4 1
    1 3 2
    2 3 0
    1 5 6
    7 2
    5 6 16
    5 7 4
    
    예상 출력
    28
    20