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.

63 lines
1.5 KiB

2 years ago
#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; // 出发点
2 years ago
int dis[N]; // 距离数组
2 years ago
bool st[N]; // Dijkstra是不是入过队列
void dijkstra() {
2 years ago
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();
2 years ago
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
}
}
}
}
int main() {
while (cin >> n >> m >> S) {
// 初始化
memset(st, 0, sizeof st);
memset(h, -1, sizeof h);
2 years ago
memset(dis, 0x3f, sizeof dis);
2 years ago
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;
2 years ago
ans = min(ans, dis[x]);
2 years ago
}
printf("%d\n", ans == INF ? -1 : ans);
}
return 0;
}