PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000068. Khôi phục mạng đường (Restoring Road Network)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Khôi phục mạng đường (Restoring Road Network)
Restoring Road Network
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Given cities and the all-pairs shortest-distance matrix , reconstruct an undirected positive-weight road network matching all distances and minimizing the sum of lengths of its roads. Output -1 if the matrix is impossible.
Input
First , then an symmetric integer matrix with zero diagonal and positive off-diagonal entries.
Output
Print the minimum total length, or -1.
Subtasks
-
Subtask 1 (40 points): . , với , .
-
Subtask 2 (60 points): . , với , .
Examples
Input
3
0 2 5
2 0 3
5 3 0
Output
5
Explanation
Road 1–2 costs 2 and road 2–3 costs 3. The distance 1–3 is already 5 via city 2, so only the two indispensable edges remain, with total weight 5.