标签:logs int main turn ios ++ cstring 题目 algorithm
题目:【模板】单源最短路径(洛谷_3371)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<vector> using namespace std; inline int read(){ int num=0,t=1;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=(num<<1)+(num<<3)+c-‘0‘;c=getchar();} return num*t; } const int N=10010,INF=0x7fffffff; struct edge{int t,c;}; vector<edge> g[N]; typedef pair<int,int> P; int d[N],n,m,s; void dij(){ priority_queue< P,vector<P>,greater<P> > q; for(int i=0;i<=n;i++)d[i]=INF; d[s]=0;q.push(P(0,s)); while(!q.empty()){ P p=q.top();q.pop(); int x=p.second; if(d[x]<p.first)continue; for(int i=0;i<g[x].size();i++){ edge e=g[x][i]; if(d[e.t]>d[x]+e.c){ d[e.t]=d[x]+e.c; q.push(P(d[e.t],e.t)); } } } } int main() { n=read();m=read();s=read(); for(int i=1;i<=m;i++){ int a=read(),b=read(),c=read(); g[a].push_back((edge){b,c}); } dij(); for(int i=1;i<=n;i++)printf("%d ",d[i]); return 0; }
本文由Yzyet编写,网址为www.cnblogs.com/Yzyet。非Yzyet同意,禁止转载,侵权者必究。
标签:logs int main turn ios ++ cstring 题目 algorithm
原文地址:http://www.cnblogs.com/Yzyet/p/7623443.html