PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000221. Sửa chữa đường sá (Road Reparation)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Sửa chữa đường sá (Road Reparation)
Road Reparation
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
There are cities and undirected roads, but none of the roads can currently be used. Each road has a known repair cost. You may repair any subset of the roads. After the repairs, every city must be reachable from every other city using repaired roads only.
Find the minimum possible total repair cost. Report impossibility if no such subset exists.
Input
- The first line contains integers : the number of cities and roads.
- Each of the next lines contains , describing a two-way road between cities with repair cost . Cities are numbered from to . There are no self-loops or parallel roads.
Output
Print the minimum total repair cost, or IMPOSSIBLE if the cities cannot all be connected.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; đường không trùng.
- Subtask 2 (30 points): ; ; ; ; ; đường không trùng.
- Subtask 3 (50 points): ; ; ; ; ; đường không trùng.
Examples
Input
5 6
1 2 3
2 3 5
2 4 2
3 4 8
5 1 7
5 4 4
Output
14
Explanation
Choose roads of cost , of cost , of cost , and of cost . They connect all five cities, costing .