Files
python/TangDou/AcWing_TiGao/T3/MiniPathExtend/1137_2.cpp

63 lines
1.5 KiB
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int M = 2e5 + 5, N = 1005;
// 存图
int idx, h[N], e[M], w[M], ne[M];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int n, m; // n个点,m条边
int S; // 出发点
int dis[N]; // 距离数组
bool st[N]; // Dijkstra是不是入过队列
void dijkstra() {
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, S});
dis[S] = 0;
while (q.size()) {
auto t = q.top();
int u = t.second;
q.pop();
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (dis[j] > dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
q.push({dis[j], j});
}
}
}
}
int main() {
while (cin >> n >> m >> S) {
// 初始化
memset(st, 0, sizeof st);
memset(h, -1, sizeof h);
memset(dis, 0x3f, sizeof dis);
idx = 0;
int ans = INF;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(b, a, c); // 反向建边
}
// 最短路
dijkstra();
int T; // T个终点
int x; // 终点ID
cin >> T;
while (T--) {
cin >> x;
ans = min(ans, dis[x]);
}
printf("%d\n", ans == INF ? -1 : ans);
}
return 0;
}