1. 程式人生 > >洛谷 P2910 [USACO08OPEN]尋寶之路Clear And Present Danger

洛谷 P2910 [USACO08OPEN]尋寶之路Clear And Present Danger

++ 之間 找到 ota lar isl all 最短路 urn

        洛谷 P2910 [USACO08OPEN]尋寶之路Clear And Present Dange

題意翻譯

題目描述 農夫約翰正駕駛一條小艇在牛勒比海上航行.

海上有N(1≤N≤100)個島嶼,用1到N編號.約翰從1號小島出發,最後到達N號小島.

一張藏寶圖上說,如果他的路程上經過的小島依次出現了Ai,A2,…,AM(2≤M≤10000)這樣的序列(不一定相鄰),那他最終就能找到古老的寶藏. 但是,由於牛勒比海有海盜出沒.約翰知道任意兩個島嶼之間的航線上海盜出沒的概率,他用一個危險指數Dij(0≤Dij≤100000)來描述.他希望他的尋寶活動經過的航線危險指數之和最小.那麽,在找到寶藏的前提下,這個最小的危險指數是多少呢?

輸入輸出格式 輸入格式: 第一行:兩個用空格隔開的正整數N和M

第二到第M+1行:第i+1行用一個整數Ai表示FJ必須經過的第i個島嶼

第M+2到第N+M+1行:第i+M+1行包含N個用空格隔開的非負整數分別表示i號小島到第1...N號小島的航線各自的危險指數。保證第i個數是0。

輸出格式 第一行:FJ在找到寶藏的前提下經過的航線的危險指數之和的最小值。

說明 這組數據中有三個島嶼,藏寶圖要求FJ按順序經過四個島嶼:1號島嶼、2號島嶼、回到1號島嶼、最後到3號島嶼。每條航線的危險指數也給出了:航路(1,2)、(2,3)、(3,1)和它們的反向路徑的危險指數分別是5、2、1。

FJ可以通過依次經過1、3、2、3、1、3號島嶼以7的最小總危險指數獲得寶藏。這條道路滿足了奶牛地圖的要求(1,2,1,3)。我們避開了1號和2號島嶼之間的航線,因為它的危險指數太大了。

註意:測試數據中a到b的危險指數不一定等於b到a的危險指數!

Translated by @LJC00125

題目描述

Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands conveniently labeled 1..N in the Cowribbean Sea.

The treasure map tells him that he must travel through a certain sequence A_1, A_2, ..., A_M of M (2 <= M <= 10,000) islands, starting on island 1 and ending on island N 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 A_i sequence in the order specified by the map.

FJ wants to avoid pirates and knows the pirate-danger rating (0 <= danger <= 100,000) 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.

農夫約翰正駕駛一條小艇在牛勒比海上航行.

海上有N(1≤N≤100)個島嶼,用1到N編號.約翰從1號小島出發,最後到達N號小島.

一張藏寶圖上說,如果他的路程上經過的小島依次出現了Ai,A2,…,AM(2≤M≤10000)這樣的序列(不一定相鄰),那他最終就能找到古老的寶藏. 但是,由於牛勒比海有海盜出沒.約翰知道任意兩個島嶼之間的航線上海盜出沒的概率,他用一個危險指數Dij(0≤Dij≤100000)來描述.他希望他的尋寶活動經過的航線危險指數之和最小.那麽,在找到寶藏的前提下,這個最小的危險指數是多少呢?

輸入輸出格式

輸入格式:

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Line i+1 describes the i_th island FJ must visit with a single integer: A_i

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

輸出格式:

* Line 1: 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 3 islands and the treasure map requires Farmer John to visit a sequence of 4 islands in order: island 1, island 2, island 1 again, and finally island 3. The danger ratings of the paths are given: the paths (1, 2); (2, 3); (3, 1) and the reverse paths have danger ratings of 5, 2, and 1, respectively.

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

輸入輸出格式

輸入格式:

第一行:兩個用空格隔開的正整數N和M

第二到第M+1行:第i+1行用一個整數Ai表示FJ必須經過的第i個島嶼

第M+2到第N+M+1行:第i+M+1行包含N個用空格隔開的非負整數分別表示i號小島到第1...N號小島的航線各自的危險指數。保證第i個數是0。

輸出格式

第一行:FJ在找到寶藏的前提下經過的航線的危險指數之和的最小值。

說明

這組數據中有三個島嶼,藏寶圖要求FJ按順序經過四個島嶼:1號島嶼、2號島嶼、回到1號島嶼、最後到3號島嶼。每條航線的危險指數也給出了:航路(1,2)、(2,3)、(3,1)和它們的反向路徑的危險指數分別是5、2、1。

FJ可以通過依次經過1、3、2、3、1、3號島嶼以7的最小總危險指數獲得寶藏。這條道路滿足了奶牛地圖的要求(1,2,1,3)。我們避開了1號和2號島嶼之間的航線,因為它的危險指數太大了。

註意:測試數據中a到b的危險指數不一定等於b到a的危險指數!

思路:Floyd 求最短路 難度:普及-

(學弟學妹們剛學最短路,來發幾篇題解增增閱讀量 嘻嘻)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m, a[10001], sum;
int dis[101][101];
int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++)
        scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            scanf("%d", &dis[i][j]);
    for(int k = 1; k <= n; k++)        //Floyd核心代碼
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(dis[i][j] > dis[i][k]+dis[k][j] && i != j)
                    dis[i][j] = dis[i][k]+dis[k][j];
    for(int i = 1; i < m; i++)
        sum += dis[a[i]][a[i+1]];
    printf("%d", sum);
    return 0;
}

洛谷 P2910 [USACO08OPEN]尋寶之路Clear And Present Danger