标签:head pen node main == pre display str end
突然意识到以前写的都是假的dij,感谢GhostCai神犇。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define int long long using namespace std; const int MAXN = 10005; const int MAXM = 500005; const int inf = 2147483647; inline int rd(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)) {f=ch==‘-‘?0:1;ch=getchar();} while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} return f?x:-x; } int n,m,S,dis[MAXN],head[MAXN],cnt; int to[MAXM],nxt[MAXM],val[MAXM]; bool vis[MAXN]; struct Node{ int id,w; friend bool operator<(const Node A,const Node B){ return A.w>B.w; } }; inline void add(int bg,int ed,int w){ to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt,val[cnt]=w; } priority_queue<Node> Q; void dijkstra(){ for(int i=1;i<=n;i++) dis[i]=inf; Node now;now.id=S;now.w=0;dis[S]=0; int p,ww;Q.push(now); while(!Q.empty()){ Node x=Q.top();Q.pop();p=x.id;ww=x.w; if(vis[p] || dis[p]!=ww) continue; vis[p]=1;int u; for(register int i=head[p];i;i=nxt[i]){ u=to[i]; if(dis[p]+val[i]<dis[u]) { dis[u]=dis[p]+val[i]; now.id=u;now.w=dis[u]; Q.push(now); } } } } signed main(){ n=rd(),m=rd(),S=rd();int x,y,z; for(int i=1;i<=m;i++){ x=rd(),y=rd(),z=rd(); add(x,y,z); } dijkstra(); for(int i=1;i<=n;i++) printf("%lld ",dis[i]); return 0; }
标签:head pen node main == pre display str end
原文地址:https://www.cnblogs.com/sdfzsyq/p/9737531.html