0pts求调(玄关

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

C417 @ 2024-10-25 17:41:20

调了3.5h了还是0pts,有大佬能帮帮忙看看哪里有问题吗

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ft ll
ft qread(){ft f=1, a=0;char ch;ch=getchar();for(; !isdigit(ch); ch=getchar()){if(ch=='-'){f=-1;}}for(; isdigit(ch); ch=getchar()){a=a*10+ch-'0';}return a*f;}
void write(ft x){if(x<0){putchar('-');x=-x;}if(x<10){putchar(x+'0');return ;}write(x/10);putchar(x%10+'0');}
using namespace std;
const int N=1e6+5, MAX=5000, INF=0x3f3f3f3f, MOD=1e9;
pair<int, int> t;
priority_queue<pair<int, int> > q;
struct edge{
    int to, nxt, w;
}e[N];
int vis[N], head[N], idx;
int n, m, s, w, u, v, dis[N];
void dij(){
    for(int i=1; i<=n; i++)
        dis[i]=INT_MAX;
    t.first=0;
    t.second=s;
    dis[s]=0;
    q.push(t);
    while(q.size()){
        u=q.top().second;
        q.pop();
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=head[u]; i; i=e[i].nxt){
            v=e[i].to;
            w=e[i].w;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                q.push(make_pair(-dis[v], v));
            }
        }
    }
}
void add(int u, int v, int w){
    idx++;
    e[idx].nxt=head[u];
    e[idx].to=v;
    e[idx].w=w;
}
int main(){
    memset(vis, 0, sizeof(vis));
    cin>>n>>m>>s;
    for(int i=0; i<m; i++){
        cin>>u>>v>>w;
        add(u, v, w);
    }
    dij();
    for(int i=1; i<=n; i++)
        cout<<dis[i]<<" ";
    cout<<endl;
    return 0;
}

by sxshm @ 2024-10-25 18:34:34

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ft ll
ft qread(){ft f=1, a=0;char ch;ch=getchar();for(; !isdigit(ch); ch=getchar()){if(ch=='-'){f=-1;}}for(; isdigit(ch); ch=getchar()){a=a*10+ch-'0';}return a*f;}
void write(ft x){if(x<0){putchar('-');x=-x;}if(x<10){putchar(x+'0');return ;}write(x/10);putchar(x%10+'0');}
using namespace std;
const int N=1e6+5, MAX=5000, INF=0x3f3f3f3f, MOD=1e9;
pair<int, int> t;
priority_queue<pair<int, int> > q;
struct edge{
    int to, nxt, w;
}e[N];
int vis[N], head[N], idx;
int n, m, s, w, u, v, dis[N];
void dij(){
    for(int i=1; i<=n; i++)
        dis[i]=INT_MAX;
    t.first=0;
    t.second=s;
    dis[s]=0;
    q.push(t);
    while(q.size()){
        u=q.top().second;
        q.pop();
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=head[u]; i; i=e[i].nxt){

            v=e[i].to;
            w=e[i].w;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                q.push(make_pair(-dis[v], v));
            }
        }
    }
}
void add(int u, int v, int w){
    idx++;
    e[idx].to=v;
    e[idx].w=w;
    e[idx].nxt=head[u];
    head[u]=idx;//少了这一句
}
int main(){
    memset(vis, 0, sizeof(vis));
    cin>>n>>m>>s;
    for(int i=0; i<m; i++){
        cin>>u>>v>>w;
        add(u,v,w);
    }
    dij();
    for(int i=1; i<=n; i++)
        cout<<dis[i]<<" ";
    cout<<endl;
    return 0;
}

by sxshm @ 2024-10-25 18:34:57

@C417


by C417 @ 2024-10-25 22:02:06

@sxshm 谢谢大佬


by C417 @ 2024-10-25 22:04:17

@sxshm 已关


by C417 @ 2024-10-25 22:04:43

此贴结


|