题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 800, M = 3000, INF = 0x3f3f3f3f;
int h[N], e[M], w[M], ne[M], idx;
int id[N];
bool st[N];
int dist[N];
int n, p, c;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a]= idx ++;
}
int spfa(int start)
{
memset(dist, 0x3f, sizeof dist);
queue<int> q;
q.push(start);
st[start] = true;
dist[start] = 0;
while(q.size())
{
auto t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i], d = w[i];
if (dist[j] > dist[t] + d)
{
dist[j] = dist[t] + d;
if (!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
int res = 0;
for (int i = 1; i <= n; i ++)
{
int j = id[i];
if (dist[j] == INF) return INF;
res += dist[j];
}
return res;
}
int main()
{
cin >> n >> p >> c;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++) cin >> id[i];
for (int i = 1; i <= c; i ++)
{
int a, b, w;
cin >> a >> b >> w;
add(a, b, w);
add(b, a, w);
}
int res = INF;
for (int i = 1; i <= p; i ++)
{
res = min(res, spfa(i));
}
cout << res;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla