Dij板子求助

P1342 请柬

QZY2008 @ 2021-10-22 22:17:05

萌新刚学OI1秒钟。望大佬帮助

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e6+1;
struct Node{
    ll to,nxt;
    ll value;
};
Node edge[N<<2];
ll head[N<<2],tot=0;
ll dis[N<<2];
struct node{
    ll fa;
    ll dis;
    bool operator <(const node x)const{
        return x.dis<dis;
    }
};
inline void add_edge(ll u,ll v,ll value){
    ++tot;
    edge[tot].nxt=head[u];
    edge[tot].value=value;
    edge[tot].to=v;
    head[u]=tot;
}
ll n,m,s,t;
bool vis[N<<2];
priority_queue<node>Q;
inline ll read(){
    ll x=0,f=1;
    char ch=getchar();
    for (;!isdigit(ch);ch=getchar())
        if (x=='-')f=-1;
    for (;isdigit(ch);ch=getchar())
        x=(x<<1)+(x<<3)+(ch^48);
    return x*f;
}
inline void dijkstra(){
    dis[s]=0; Q.push((node){0,s});
    while (!Q.empty()){
        node tmp=Q.top();
        Q.pop();
        ll cnt=tmp.fa;
        if (vis[cnt])continue;
        vis[cnt]=1;
        for (ll i=head[cnt];i;i=edge[i].nxt){
            ll res=edge[i].to;
            if (dis[res]>dis[cnt]+edge[i].value){
                dis[res]=dis[cnt]+edge[i].value;
                if(!vis[res])
                    Q.push((node){dis[res],res});
            }
        } 
    }
}
int main(){
    n=read(),m=read(),s=1,t=n;
    for (ll _=1;_<=n;_++)
        dis[_]=INT_MAX;
    for (ll _=1;_<=m;_++){
        ll u=read(),v=read(),val=read();
        add_edge(u,v,val);
    }
    dijkstra();
    printf("%lld\n",dis[t]);
}

by ¶凉笙 @ 2021-10-22 22:26:19

你的堆是大根堆,堆 < 的定义比较特殊


by QZY2008 @ 2021-10-23 06:22:02

@¶凉笙 什么意思啊,求解谢谢、


|