Files
python/TangDou/Topic/P1828.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 814;
const int INF = 0x3f3f3f3f;
int id[N];
int a, b, c, g[N][N], res = INF;
int main() {
#ifndef ONLINE_JUDGE
freopen("P1828_11.in", "r", stdin);
// 参考答案8
#endif
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
int p, n, m; // p只奶牛n个牧场m条边
cin >> p >> n >> m;
memset(g, 0x3f, sizeof g);
for (int i = 1; i <= n; i++) g[i][i] = 0; // 初始化
for (int i = 1; i <= p; i++) cin >> id[i]; // i号奶牛在id[i]这个牧场
while (m--) {
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(c, g[a][b]);
}
// 标准的Floyd板子
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++) {
if (g[i][k] == INF) continue; // floyd小优化
for (int j = 1; j <= n; j++)
if (g[i][j] > g[i][k] + g[k][j])
g[j][i] = g[i][j] = g[i][k] + g[k][j];
}
for (int i = 1; i <= n; i++) { // 每个牧场出发
int ans = 0;
for (int j = 1; j <= p; j++) ans += g[i][id[j]];
if (ans >= 0) res = min(res, ans);
}
printf("%d", res);
return 0;
}