Most Scenic Cycle

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

요약
강하게 연결된 다중 그래프에서 각 간선에 가중치가 주어질 때 최대 가중치를 갖는 단순 사이클을 구한다.
난이도

어려움10점 중 9점

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

문제

The government of the Independent Country of Problem Creators (ICPC) finally saved enough money to construct high speed rail infrastructure. The new rail system has VV stations and EE bidirectional direct railway lines that each connect two stations together. The head of ICPC Rail Infrastructure Planning, Skib E. Dee, has seen enough programming problems about tree-topology transportation networks in other countries to know that such a network would be a recipe for disaster: a single broken railway line would split the network into disconnected pieces and disrupt travel for days. Instead, Dee decided that the ICPC rail network will be robustly connected: every pair of stations s_1s\_1, s_2s\_2 must be connected by at least two paths which do not share any direct railway lines, and do not share any railway stations other than s_1s\_1 and s_2s\_2 themselves.

Of course, ICPC cannot afford to build too many redundant railway lines. To balance efficiency and resiliency, Dee has also designed the network to be regionally connected. A cycle is a non-empty path from a station to itself which doesn't repeat any railway station (apart from the first station, which must repeat exactly once as the last station of the cycle). In order for the network to be regionally connected, there must exist a set F\mathcal{F} of E−V+1E-V+1 regional cycles satisfying three properties:

  • every direct railway line in the transportation network belongs to at least one regional cycle;
  • if two regional cycles share any direct railway lines, then all railway lines and stations shared by those cycles lie along a connected path;
  • for each subset ff of F\mathcal{F}, at most ∣f∣−1|f|-1 pairs of regional cycles in ff share any direct railway lines.

To promote the new high speed rail, Dee needs to create a timelapse video of a train travelling around some cycle in the railway network. Each direct railway line has a (possibly negative) scenic value representing how nice the view out the train window is along that line. Dee wants to send the train around whichever cycle maximizes the sum of scenic values of the direct railway lines on the cycle---compute this maximum possible sum. (The most scenic cycle that Dee is looking for does not have to be a regional cycle.) In order to ensure this cycle is not boring, it must traverse at least two direct railway lines, and must not repeat any railway station (apart from the first station, which must repeat exactly once as the last station of the cycle).

입력

The first line of input contains two space-separated integers VV (2≤V≤2⋅1052\leq V \leq 2 \cdot 10^5) and EE (V≤E≤4⋅105V \leq E \leq 4 \cdot 10^5), the number of stations and direct railway lines in the rail network, respectively.

The next EE lines of input describe the direct railway lines. Each line contains three space-separated integers aa, bb, and ss (1≤a,b≤V1\leq a,b \leq V; −109≤s≤109-10^9 \leq s \leq 10^9), signifying that a direct railway line exists between stations aa and bb with scenic value ss. No direct railway line connects a station to itself, but multiple direct railway lines might exist between the same two stations. It is guaranteed that the input graph will be both robustly connected and regionally connected.

출력

Print the sum of scenic values around the cycle in the railway network that maximizes this sum.

예제2

  1. 예제 1

    입력
    6 9
    1 2 9
    2 3 9
    3 4 9
    3 4 -9
    4 1 9
    1 5 1
    5 6 1
    6 2 1
    3 4 8
    
    예상 출력
    36
    
  2. 예제 2

    입력
    5 7
    1 2 1
    2 3 -2
    3 4 3
    4 5 6
    5 1 4
    5 3 2
    2 5 9
    
    예상 출력
    16