Files
python/TangDou/AcWing_TiGao/T3/MiniPathExtend/1137_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

62 lines
1.8 KiB
C++
Raw 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;
typedef pair<int, int> PII;
// 建立虚拟源点0
const int N = 1010, M = 40010;
const int INF = 0x3f3f3f3f;
int n, m, S;
// 邻接表
int h[N], e[M], w[M], ne[M], idx;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int dis[N]; // 最短距离数组
bool st[N]; // 是否进过队列
// 迪杰斯特拉
void dijkstra() {
memset(dis, 0x3f, sizeof dis); // 初始化大
memset(st, 0, sizeof st); // 初始化为未出队列过
priority_queue<PII, vector<PII>, greater<PII>> q; // 小顶堆
q.push({0, 0}); // 出发点入队列
dis[0] = 0; // 出发点距离0
while (q.size()) {
auto t = q.top();
q.pop();
int u = t.second;
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});
}
}
}
// 注意:此处的S是终点,不是起点,不是起点,不是起点!
printf("%d\n", dis[S] == INF ? -1 : dis[S]);
}
int main() {
while (cin >> n >> m >> S) {
// 注意清空链表头
memset(h, -1, sizeof h);
idx = 0;
// m条边
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int T;
cin >> T;
while (T--) {
int x;
cin >> x;
add(0, x, 0); // 超级源点法
}
dijkstra();
}
return 0;
}