# bzoj1624:[Usaco2008 Open] Clear And Present Danger 寻宝之路

## 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.

floyd不解释。

1 #include<cstdio>
2 int x[10001],g[101][101],ans,n,m,i,j,k;
3 int main()
4 {
5     scanf("%d%d",&n,&m);
6     for(i=1;i<=m;i++)
7         scanf("%d",&x[i]);
8     for(i=1;i<=n;i++)
9         for(j=1;j<=n;j++)
10             scanf("%d",&g[i][j]);
11     for(k=1;k<=n;k++)
12         for(i=1;i<=n;i++)
13             for(j=1;j<=n;j++)
14                 if(g[i][j]>g[i][k]+g[k][j])
15                     g[i][j]=g[i][k]+g[k][j];
16     for(i=2;i<=m;i++)ans+=g[x[i-1]][x[i]];
17     ans+=g[1][x[1]]+g[x[m]][n];
18     printf("%d",ans);
19 }

posted @ 2018-08-08 11:13  蒟蒻--lichenxi  阅读(80)  评论(0编辑  收藏  举报