社区讨论
最短路60wa求调
学术版参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lo15dzrg
- 此快照首次捕获于
- 2023/10/22 15:28 2 年前
- 此快照最后确认于
- 2023/11/02 15:00 2 年前
P3371 【模板】单源最短路径(弱化版)
CPP#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
int n, m, hand[N], tot, x, y, v, s;
long long dis[N];
bool ff[N];
typedef pair <int, int> pii;
priority_queue <pii, vector< pii >, greater < pii > >q;
struct liu {
int w, to, next;
} e[N];
void add(int x, int y, int v) {
e[++tot].next = hand[x];
e[tot].to = y;
e[tot].w = v;
hand[x] = tot;
}
void dijk(int s) {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
q.push({0, s});
while (!q.empty()) {
auto t = q.top();
q.pop();
int x = t.first, y = t.second;
if (ff[y])continue;
ff[y] = true;
for (int i = hand[y]; i; i = e[i].next) {
if (dis[e[i].to] > dis[y] + e[i].w) {
dis[e[i].to] = dis[y] + e[i].w;
q.push({dis[e[i].to], e[i].to});
}
}
}
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= m ; i++) {
cin >> x >> y >> v;
add(x, y, v);
}
dijk(s);
for (int i = 1; i <= n; i++) {
if (dis[i] != 0x3f)cout << dis[i] << " ";
else cout << 2147483647 << " ";
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...