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.8 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++;
}
2 years ago
int dis[N]; // 最短距离数组
2 years ago
bool st[N]; // 是否进过队列
// 迪杰斯特拉
void dijkstra() {
2 years ago
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
2 years ago
2 years ago
while (q.size()) {
auto t = q.top();
q.pop();
2 years ago
int u = t.second;
if (st[u]) continue;
st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
2 years ago
if (dis[j] > dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
q.push({dis[j], j});
2 years ago
}
}
}
// 注意此处的S是终点不是起点不是起点不是起点
2 years ago
printf("%d\n", dis[S] == INF ? -1 : dis[S]);
2 years ago
}
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;
2 years ago
cin >> T;
2 years ago
while (T--) {
int x;
cin >> x;
2 years ago
add(0, x, 0); // 超级源点法
2 years ago
}
dijkstra();
}
return 0;
}