1. 程式人生 > >BZOJ 1624--Clear And Present Danger 尋寶之路(最短路)

BZOJ 1624--Clear And Present Danger 尋寶之路(最短路)

path clear rip use farmer clu href nal rem

1624: [Usaco2008 Open] Clear And Present Danger 尋寶之路

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 831 Solved: 540
[Submit][Status][Discuss]

Description

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

Input

第1行輸入N和M,之後M行一行一個整數表示A序列,之後輸入一個NxN的方陣,表示兩兩島嶼之間航線的危險指數.數據保證Dij=Dji,Dii=0.

Output

最小的危險指數和.

Sample Input

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

INPUT DETAILS:

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.


Sample Output

7

OUTPUT DETAILS:

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.

題目鏈接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=1624

Solution

    Floyd裸題。。。

代碼

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
int n,m,u,v,h;
int d[10005];
int mp[105][105];
int main(){
    int ans=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&d[i]);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            scanf("%d",&mp[i][j]);
    }
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        if(i!=k&&i!=j&&j!=k){
            if(mp[i][k]!=-1&&mp[k][j]!=-1){
                if(mp[i][j]==-1)
                    mp[i][j]=mp[i][k]+mp[k][j];
                else if(mp[i][k]+mp[k][j]<mp[i][j])
                    mp[i][j]=mp[i][k]+mp[k][j];
            }
        }
    for(int i=1;i<m;i++)
        ans+=mp[d[i]][d[i+1]];
    printf("%d\n",ans);
    return 0;
}

  

  

This passage is made by Iscream-2001.

BZOJ 1624--Clear And Present Danger 尋寶之路(最短路)