题目
P2935 [USACO09JAN]最好的地方Best Spot
分析
Floyd模板题
AC代码
#include <iostream>
using namespace std;
int n, m, p;
int a[505], f[505][505];
int main()
{
scanf("%d%d%d", &n, &m, &p);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
f[i][j] = 0x3f3f3f3f;
}
f[i][i] = 0;
}
for (int i = 1; i <= m; i++)
{
scanf("%d", &a[i]);
}
for (int i = 1; i <= p; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
f[u][v] = w;
f[v][u] = w;
}
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
}
// 枚举
int ans, MIN = 0x3f3f3f3f;
for (int i = 1; i <= n; i++)
{
int sum = 0;
for (int j = 1; j <= m; j++)
{
sum += f[i][a[j]];
}
if (sum < MIN)
{
MIN = sum;
ans = i;
}
}
cout << ans << endl;
return 0;
}