P3371 【模板】单源最短路径

来源:互联网 发布:qq影音windows版电脑版 编辑:程序博客网 时间:2024/05/16 08:42
整天被板子题卡卡,没救了。
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 secondconst int N=10005,NN=500005;priority_queue<pii,vector<pii>,greater<pii> >pq;int n,m,S,d[N];bool vis[N];int he[N],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;}

原创粉丝点击