社区讨论

关于最短路 || 跪求这是什么迷惑写法??

学术版参与者 3已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lo34y25f
此快照首次捕获于
2023/10/24 00:52
2 年前
此快照最后确认于
2023/10/24 00:52
2 年前
查看原帖
C
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> P;
priority_queue <P, vector <P>, greater <P>> q;
struct edge {int to, c;};
vector <edge> G[100005];
int n, m, dist[100005], s;
inline void dij() {
    memset(dist, 0x3f3f3f3f, sizeof(dist));
    dist[s] = 0;
    P st;
    st.first = 0;
    st.second = s;
    q.push(st);
    while (!q.empty()) {
        P fa = q.top();
        q.pop();
        int v = fa.second;
        if (dist[v] < fa.first) continue; // 似乎是这里迷惑
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (dist[e.to] > dist[v] + e.c) {
                dist[e.to] = dist[v] + e.c;
                P nxt;
                nxt.first = dist[e.to];
                nxt.second = e.to;
                q.push(nxt);
            }
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> s;
    while (m--) {
        int a;
        edge b;
        cin >> a >> b.to >> b.c;
        G[a].push_back(b);
    }
    dij();
    for (int i = 1; i <= n; i++) cout << dist[i] << " ";
    return 0;
}
rt
实测标准版数据可过,
求问
Q1Q1 上述代码中标注的那一句有啥作用?
Q2Q2 这是什么迷惑写法,是否可信?

回复

5 条回复,欢迎继续交流。

正在加载回复...