优先队列Dijkstra只过了第五点,悬关xddd

P4779 【模板】单源最短路径(标准版)

Crushxl @ 2023-08-13 18:15:14

代码可以通过样例和第五个测试点,其它测试点全WA

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int n,m,s,cnt,head[MAXN];

struct Edge{int next,to,w;}e[MAXN];

void AddEdge(int u,int v,int w){
    e[++cnt].next = head[u];
    e[cnt].to = v;
    e[cnt].w = w;
    head[u] = cnt;
}

struct Node{
    int to,dis;
    bool operator<(const Node a)const{a.dis<dis;}
};

int check[MAXN],dis[MAXN];
void Dijkstra(){
    memset(dis,0x3f3f3f3f,sizeof(dis));
    dis[s] = 0;
    priority_queue<Node> q;
    q.push({s,0});
    while(!q.empty()){
        Node tmp = q.top();
        q.pop();
        int x = tmp.to;
        if(!check[x]){
            for(int i=head[x];i!=0;i=e[i].next){
                int y = e[i].to;
                if(dis[y] > dis[x]+e[i].w){
                    dis[y] = dis[x]+e[i].w;
                    q.push({y,dis[y]});
                }
            }
        }
        check[x] = 1;
    }
}

int main(){
    cin >> n >> m >> s;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin >> u >> v >> w; 
        AddEdge(u,v,w);
    }
    Dijkstra();
    for(int i=1;i<=n;i++){
        cout << dis[i] << " ";
    }
    return 0;
}

by Crushxl @ 2023-08-13 18:53:55

@shalu 未被更新的点不会有机会标记,这里似乎不加判断也可以AC


by shalu @ 2023-08-13 18:54:57

@Crushxl 我个人习惯用vector,可能写习惯了,谢谢提醒


by Crushxl @ 2023-08-13 18:55:54

@fangzichang 谢谢谢谢!改完之后代码AC了,已关注xdd


by Xile @ 2023-08-13 18:58:36

@Crushxl 可以学学pair,很好用的。我学这么久了还是不会STL的重载(


by Crushxl @ 2023-08-13 18:58:53

@SuperChao 谢谢您!关注了!


上一页 |