当前位置: 代码迷 >> 综合 >> 洛谷 P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
  详细解决方案

洛谷 P2910 [USACO08OPEN]寻宝之路Clear And Present Danger

热度:15   发布时间:2023-12-06 08:15:49.0

题目:寻宝之路

思路:用floyd求出任意两点最短路,再根据输入求和。

代码:

#include<bits/stdc++.h>
using namespace std;#define maxn 100
#define maxm 10000int n,m;
vector<int> pb;
int dist[maxn+5][maxn+5]={
   0};void readin(){scanf("%d%d",&n,&m);for(int i=1;i<=m;i++) {int x;scanf("%d",&x);pb.push_back(x);}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) {scanf("%d",&dist[i][j]);}}
}void floyd(){for(int k=1;k<=n;k++) {for(int i=1;i<=n;i++) {for(int j=1;j<=n;j++) {dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);}}}
}void print(){int ans=0;for(int i=1;i<pb.size();i++){int x=pb[i-1],y=pb[i];ans+=dist[x][y];}printf("%d",ans);
}int main(){readin();floyd();print();return 0;
}
  相关解决方案