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

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

题意:

求按点1-a1-a2...-an-n走的最短路长度是多少。点数小于等于100。

题解:

floyd。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 110
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 int map[maxn][maxn],a[maxn*100],n,m,ans;
15 int main(){
16     n=read(); m=read(); inc(i,1,m)a[i]=read(); inc(i,1,n)inc(j,1,n)map[i][j]=read();
17     inc(k,1,n)inc(i,1,n)inc(j,1,n)if(map[i][k]+map[k][j]<map[i][j])map[i][j]=map[i][k]+map[k][j];
18     ans+=map[1][a[1]]; inc(i,2,m)ans+=map[a[i-1]][a[i]]; ans+=map[a[m]][n]; printf("%d",ans); return 0;
19 }

 

20160804

posted @ 2016-08-06 14:23  YuanZiming  阅读(134)  评论(0编辑  收藏  举报