#abc218e. E - Destruction

E - Destruction

Score : 500500 points

问题描述

我们有一个包含 NN 个顶点和 MM 条边的连通无向图。
顶点编号从 11NN,边编号从 11MM。边 ii 连接顶点 AiA_iBiB_i

高桥将从此图中移除零个或多个边。

当移除边 ii 时,若 Ci0C_i \geq 0,则会获得奖励 CiC_i;若 Ci<0C_i<0,则会产生罚款 Ci|C_i|

求在移除边后图形必须保持连通的前提下,高桥可以获得的最大总奖励是多少。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a connected undirected graph with NN vertices and MM edges.
The vertices are numbered 11 through NN, and the edges are numbered 11 through MM. Edge ii connects Vertices AiA_i and BiB_i.

Takahashi is going to remove zero or more edges from this graph.

When removing Edge ii, a reward of CiC_i is given if Ci0C_i \geq 0, and a fine of Ci|C_i| is incurred if Ci<0C_i<0.

Find the maximum total reward that Takahashi can get when the graph must be connected after removing edges.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • N1M2×105N-1 \leq M \leq 2\times 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 109Ci109-10^9 \leq C_i \leq 10^9
  • The given graph is connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1 C1C_1

A2A_2 B2B_2 C2C_2

\vdots

AMA_M BMB_M CMC_M

Output

Print the answer.

Sample Input 1

4 5
1 2 1
1 3 1
1 4 1
3 2 2
4 2 2

Sample Output 1

4

Removing Edges 44 and 55 yields a total reward of 44. You cannot get any more, so the answer is 44.

Sample Input 2

3 3
1 2 1
2 3 0
3 1 -1

Sample Output 2

1

There may be edges that give a negative reward when removed.

Sample Input 3

2 3
1 2 -1
1 2 2
1 1 3

Sample Output 3

5

There may be multi-edges and self-loops.

update @ 2024/3/10 09:39:06