P3371 【模板】单源最短路径
2017-07-27 16:46
477 查看
整天被板子题卡卡,没救了。
dj堆优化。
dj堆优化。
#include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<algorithm> #include<set> #include<bitset> #include<queue> using namespace std; #define rep(i,j,k) for(i=j;i<=k;++i) #define per(i,j,k) for(i=j;i>=k;--i) #define G getchar() #define LL long long #define pii pair<int,int> #define mkp make_pair #define X first #define Y second const int N=10005,NN=500005; priority_queue<pii,vector<pii>,greater<pii> >pq; int n,m,S,d ;bool vis ; int he ,ne[NN],to[NN],W[NN],tot; void read(int &x){ char ch=G; while(ch<48||ch>57)ch=G; for(x=0;ch>47&&ch<58;ch=G)x=x*10+ch-48; } void add(int x,int y,int z){ to[++tot]=y;W[tot]=z;ne[tot]=he[x];he[x]=tot; } int main(){ int x,y,z,i,tmp; read(n);read(m);read(S); while(m--){ read(x);read(y);read(z); add(x,y,z); } rep(i,1,n)d[i]=2147483647; pq.push(mkp(d[S]=0,S)); while(!pq.empty()){ x=pq.top().Y;pq.pop(); if(vis[x])continue;vis[x]=1; for(i=he[x];i;i=ne[i]){ if((tmp=d[x]+W[i])<d[y=to[i]]) pq.push(mkp(d[y]=tmp,y)); } } rep(i,1,n)printf("%d ",d[i]);puts(""); return 0; }
相关文章推荐
- 洛谷 P3371 【模板】单源最短路径(Dijkstra + 堆优化)
- [洛谷]P3371 单源最短路径模板 SPFA
- [洛谷]P3371 单源最短路径模板-bell
- [洛谷]P3371 单源最短路径模板 Dijkstra
- 洛谷 P3371 【模板】单源最短路径
- P3371 【模板】单源最短路径
- 洛谷 P3371 【模板】单源最短路径
- 洛谷 P3371 【模板】单源最短路径
- P3371 【模板】单源最短路径
- 洛谷 P3371 【模板】单源最短路径
- 洛谷 P3371 【模板】单源最短路径
- P3371 【模板】单源最短路径
- P3371 【模板】单源最短路径 SPFA优化 dijkstra堆优化
- P3371 单源最短路径【模板】 洛谷
- 【模板】Spfa单源最短路径
- 洛谷 P3371 [模版] 单源最短路径
- 三解<单源最短路径模板题>
- 洛谷P3371 【模板】单源最短路径
- 【模板】单源最短路径*
- 洛谷P3371 【模板】单源最短路径