比赛 |
4043级NOIP2022欢乐赛6th |
评测结果 |
AAAATTTAAA |
题目名称 |
旅行者 |
最终得分 |
70 |
用户昵称 |
该账号已注销 |
运行时间 |
14.502 s |
代码语言 |
C++ |
内存使用 |
17.66 MiB |
提交时间 |
2022-11-18 19:10:17 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
struct edge {
int t, n, w;
} e[500100];
int T, cnt = 0, hd[500100], k;
int lv[500100], d[500100];
int ans = 0x3f3f3f3f;
bool v[500100] = {0};
void add(int x, int y, int z) {
e[++cnt].t = y;
e[cnt].w = z;
e[cnt].n = hd[x];
hd[x] = cnt;
}
void spfa(int s) {
queue<int>q;
memset(d, 0x3f, sizeof(d));
memset(v, 0, sizeof(v));
v[s] = 1;
q.push(s);
d[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
v[u] = 0;
for (int i = hd[u]; i; i = e[i].n) {
int ver = e[i].t;
if (d[ver] > d[u] + e[i].w) {
d[ver] = d[u] + e[i].w;
if (v[ver] == 0) {
q.push(ver);
v[ver] = 1;
}
}
}
}
for (int i = 1; i <= k; i++) {
if (s != lv[i])
ans = min(ans, d[lv[i]]);
}
}
int main() {
freopen("WAW.in", "r", stdin);
freopen("WAW.out", "w", stdout);
cin >> T;
while (T--) {
int n, m;
cin >> n >> m >> k;
ans = 0x3f3f3f3f;
cnt = 0;
memset(hd, 0, sizeof(hd));
for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
}
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
lv[i] = x;
}
for (int i = 1; i <= k; i++) {
spfa(lv[i]);
}
cout << ans << endl;
}
return 0;
}