#FW0000110. Geonosis (Geonosis)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Geonosis (Geonosis)
Geonosis
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
A complex has communication towers with directed communication costs given by matrix . Towers are destroyed in permutation order . Before each destruction, the required energy is the sum of shortest-path distances over all ordered pairs of distinct surviving towers; destroyed towers may not be used as intermediates. Compute the total energy over the full process.
Input
First , then an matrix with , then a permutation of giving destruction order.
Output
Print the total energy as a 64-bit integer.
Subtasks
-
Subtask 1 (40 points): . All input-format and graph conditions above apply.
-
Subtask 2 (60 points): . All input-format and graph conditions above apply.
Examples
Input
2
0 3
2 0
0 1
Output
5
Explanation
Initially both towers are active: distance 0 to 1 is 3 and 1 to 0 is 2, totaling 5. After tower 0 is removed, no pair of distinct active towers remains. The overall total is 5.