我太菜了
今天才学会堆优化的dij
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstdlib> 6 #include<cstring> 7 #include<queue> 8 #include<vector> 9 #define ll long long 10 #define inf 2147483647 11 #define MAXN 10101 12 #define P pair<int,int> 13 #define mp(a,b) make_pair(a,b) 14 using namespace std; 15 inline int read() 16 { 17 int x=0,f=1;char ch=getchar(); 18 while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();} 19 while(isdigit(ch)) {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} 20 return x*f; 21 } 22 priority_queue <P,vector<P>,greater<P> > q; 23 int n,m,s; 24 int next[MAXN*50],first[MAXN],to[MAXN*50],val[MAXN*50],dis[MAXN],cnt; 25 void add(int u,int v,int w) {next[++cnt]=first[u],first[u]=cnt,val[cnt]=w,to[cnt]=v;} 26 int main() 27 { 28 n=read(),m=read(),s=read(); 29 int a,b,c,k; 30 for(int i=1;i<=m;i++) {a=read(),b=read(),c=read();add(a,b,c);} 31 for(int i=1;i<=n;i++) dis[i]=inf;dis[s]=0; 32 q.push(mp(0,s)); 33 while(!q.empty()) 34 { 35 k=q.top().second; 36 q.pop(); 37 for(int i=first[k];i;i=next[i]) 38 if(dis[to[i]]>dis[k]+val[i]) {dis[to[i]]=dis[k]+val[i];q.push(mp(dis[to[i]],to[i]));} 39 } 40 for(int i=1;i<=n;i++) 41 printf("%d ",dis[i]); 42 }