本地不能运行提交洛谷却AC?
查看原帖
本地不能运行提交洛谷却AC?
276540
chhhr楼主2021/3/13 21:50

蒟蒻求救SOS!!

#include<iostream>
#include<vector>  
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200005;
const int INF=(1<<30);
struct Edge{
    int from, to, dist;
    Edge(int u, int v, int w):from(u), to(v), dist(w) {}
};
struct Node{
    int d, u;
    bool operator < (const Node& rhs) const { return d>rhs.d; }
};
struct Dijkstra{
    int n, m;
    vector<Edge> edges;
    vector<int> g[maxn];
    bool flag[maxn];
    int d[maxn], p[maxn];
    void init(int n){
        this->n=n;
        for(int i=0; i<=n; i++) g[i].clear();
        edges.clear();
    }
    void add(int from, int to, int dist){
        edges.push_back(Edge(from, to, dist));
        m=edges.size();
        g[from].push_back(m-1);
    }
    void dijkstra(int s){
        priority_queue<Node> pq;
        for(int i=0; i<=n; i++) d[i]=INF;
        d[s]=0;
        memset(flag, 0, sizeof(flag));
        pq.push((Node){0, s});
        while(!pq.empty()){
            auto x=pq.top();    pq.pop();
            int u=x.u;
            if(flag[u]) continue;
            flag[u]=true;
            for(int i=0; i<g[u].size(); i++){
                Edge &e=edges[g[u][i]];
                if(d[e.to]>d[u]+e.dist){
                    d[e.to]=d[u]+e.dist;
                    p[e.to]=g[u][i];
                    pq.push((Node){d[e.to], e.to});
                }
            }
        }
    }
};
inline int qread(){
    int f=1, w=0;
    char c=getchar();
    while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); }
    while(c>='0'&&c<='9') { w=w*10-'0'+c; c=getchar(); }
    return f*w;
}
int main(){
    Dijkstra solution;
    int n=qread(), m=qread(), s=qread();
    solution.init(n);
    for(int i=1; i<=m; i++){
        int u=qread(), v=qread(), w=qread();
        solution.add(u, v, w);
    }
    solution.dijkstra(s);
    for(int i=1; i<=n; i++) cout<<solution.d[i]<<" ";
    return 0;
}
2021/3/13 21:50
加载中...