【BZOJ1624】[Usaco2008 Open] Clear And Present Danger 寻宝之路【Floyd】

【题目链接】

最后一个水题,今天不写水题了...

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 105, maxm = 10005;

int n, m, id[maxm], g[maxn][maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

int main() {
	n = iread(); m = iread();
	for(int i = 1; i <= m; i++) id[i] = iread();
	for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) g[i][j] = iread();

	for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++)
		g[i][j] = min(g[i][j], g[i][k] + g[k][j]);

	LL ans = 0;
	for(int i = 2; i <= m; i++) ans += g[id[i - 1]][id[i]];
	printf("%lld\n", ans);
	return 0;
}


你可能感兴趣的:(floyd)