logo AlgoBeat OnlineJudge
登录 注册

#201970. [USACO08OPEN] Clear And Present Danger S

内存限制:125 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Farmer John is on a boat seeking fabled treasure on one of the islands conveniently labeled in the Cowribbean Sea.

The treasure map tells him that he must travel through a certain sequence of islands, starting on island and ending on island before the treasure will appear to him. He can visit these and other islands out of order and even more than once, but his trip must include the sequence in the order specified by the map.

FJ wants to avoid pirates and knows the pirate-danger rating between each pair of islands. The total danger rating of his mission is the sum of the danger ratings of all the paths he traverses.

Help Farmer John find the least dangerous route to the treasure that satisfies the treasure map's requirement.

输入格式

Line : Two space-separated integers: and .

Lines : Line describes the -th island FJ must visit with a single integer: .

Lines : Line contains space-separated integers that are the respective danger rating of the path between island i and islands , and , respectively. The ith integer is always zero.

输出格式

Line : The minimum danger that Farmer John can encounter while obtaining the treasure.

样例

样例输入 1

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

样例输出 1

7 

数据范围与提示

There are islands and the treasure map requires Farmer John to visit a sequence of islands in order: island , island , island again, and finally island . The danger ratings of the paths are given: the paths and the reverse paths have danger ratings of , and , respectively.

He can get the treasure with a total danger of by traveling in the sequence of islands , and . The cow map's requirement (, and ) is satisfied by this route. We avoid the path between islands and because it has a large danger rating.