您的位置:首页 > 其它

P3371 【模板】单源最短路径

2017-07-27 16:46 477 查看
整天被板子题卡卡,没救了。
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: