PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000076. Bảo vệ vương quốc Rohan (Defend the Rohan)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Bảo vệ vương quốc Rohan (Defend the Rohan)
Defend the Rohan
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given a directed complete matrix of army-movement costs between stations and relocation orders , compute the minimum total travel cost by allowing intermediate stations.
Input
First , then an nonnegative directed cost matrix with zero diagonal, then orders (1-based).
Output
Print the minimum total relocation cost.
Subtasks
-
Subtask 1 (40 points): . , , ; ma trận có hướng.
-
Subtask 2 (60 points): . , , ; ma trận có hướng.
Examples
Input
3
0 1 5
1 0 2
5 2 0
2
1 3
3 1
Output
6
Explanation
The cheapest route 1→3 is 1→2→3 of cost 3; the opposite order also costs 3. The two relocation orders sum to 6.