You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB

2 years ago
#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 d[N]; // 最短距离数组
bool st[N]; // 是否进过队列
// 迪杰斯特拉
void dijkstra() {
memset(d, 0x3f, sizeof d); // 初始化大
memset(st, 0, sizeof st); // 初始化为未出队列过
priority_queue<PII, vector<PII>, greater<PII>> pq; // 小顶堆
pq.push({0, 0}); // 出发点入队列
d[0] = 0; // 出发点距离0
while (pq.size()) {
auto t = pq.top();
pq.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 (d[j] > d[u] + w[i]) {
d[j] = d[u] + w[i];
pq.push({d[j], j});
}
}
}
// 注意此处的S是终点不是起点不是起点不是起点
printf("%d\n", d[S] == INF ? -1 : d[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;
scanf("%d", &T);
while (T--) {
int x;
cin >> x;
add(0, x, 0);
}
dijkstra();
}
return 0;
}