|
|
|
@ -87,32 +87,32 @@ 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]; // 最短距离数组
|
|
|
|
|
int dis[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();
|
|
|
|
|
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 (d[j] > d[u] + w[i]) {
|
|
|
|
|
d[j] = d[u] + w[i];
|
|
|
|
|
pq.push({d[j], j});
|
|
|
|
|
if (dis[j] > dis[u] + w[i]) {
|
|
|
|
|
dis[j] = dis[u] + w[i];
|
|
|
|
|
q.push({dis[j], j});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 注意:此处的S是终点,不是起点,不是起点,不是起点!
|
|
|
|
|
printf("%d\n", d[S] == INF ? -1 : d[S]);
|
|
|
|
|
printf("%d\n", dis[S] == INF ? -1 : dis[S]);
|
|
|
|
|
}
|
|
|
|
|
int main() {
|
|
|
|
|
while (cin >> n >> m >> S) {
|
|
|
|
@ -126,11 +126,11 @@ int main() {
|
|
|
|
|
add(a, b, c);
|
|
|
|
|
}
|
|
|
|
|
int T;
|
|
|
|
|
scanf("%d", &T);
|
|
|
|
|
cin >> T;
|
|
|
|
|
while (T--) {
|
|
|
|
|
int x;
|
|
|
|
|
cin >> x;
|
|
|
|
|
add(0, x, 0);
|
|
|
|
|
add(0, x, 0); // 超级源点法
|
|
|
|
|
}
|
|
|
|
|
dijkstra();
|
|
|
|
|
}
|
|
|
|
@ -153,25 +153,25 @@ void add(int a, int b, int c) {
|
|
|
|
|
}
|
|
|
|
|
int n, m; // n个点,m条边
|
|
|
|
|
int S; // 出发点
|
|
|
|
|
int d[N]; // 距离数组
|
|
|
|
|
int dis[N]; // 距离数组
|
|
|
|
|
bool st[N]; // Dijkstra是不是入过队列
|
|
|
|
|
|
|
|
|
|
void dijkstra() {
|
|
|
|
|
priority_queue<PII, vector<PII>, greater<PII>> q;
|
|
|
|
|
q.push({0, S});
|
|
|
|
|
d[S] = 0;
|
|
|
|
|
dis[S] = 0;
|
|
|
|
|
while (q.size()) {
|
|
|
|
|
auto t = q.top();
|
|
|
|
|
int u = t.second, dist = t.first;
|
|
|
|
|
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 (d[j] > dist + w[i]) {
|
|
|
|
|
d[j] = dist + w[i];
|
|
|
|
|
q.push({d[j], j});
|
|
|
|
|
if (dis[j] > dis[u] + w[i]) {
|
|
|
|
|
dis[j] = dis[u] + w[i];
|
|
|
|
|
q.push({dis[j], j});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -181,7 +181,7 @@ int main() {
|
|
|
|
|
// 初始化
|
|
|
|
|
memset(st, 0, sizeof st);
|
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
|
memset(d, 0x3f, sizeof d);
|
|
|
|
|
memset(dis, 0x3f, sizeof dis);
|
|
|
|
|
idx = 0;
|
|
|
|
|
int ans = INF;
|
|
|
|
|
|
|
|
|
@ -197,7 +197,7 @@ int main() {
|
|
|
|
|
cin >> T;
|
|
|
|
|
while (T--) {
|
|
|
|
|
cin >> x;
|
|
|
|
|
ans = min(ans, d[x]);
|
|
|
|
|
ans = min(ans, dis[x]);
|
|
|
|
|
}
|
|
|
|
|
printf("%d\n", ans == INF ? -1 : ans);
|
|
|
|
|
}
|