dij+堆优化求单源最短路以及其模板
永远也不退役,无论有多少人比我强都不退役,关我屁事
活着就不退役
我永远肝
#include<bits/stdc++.h> using namespace std; const int maxn=1e6; int cnt=0,n,m,s,dis[maxn],vis[maxn],head[maxn]; struct mad{ int to,val,next; }edge[maxn]; struct lys { int point,val; bool operator > (const lys tmp)const{ return val>tmp.val; } }; void add(int a,int b,int val) { cnt++; edge[cnt].to=b; edge[cnt].val=val; edge[cnt].next=head[a]; head[a]=cnt; } priority_queue<lys,vector<lys>,greater<lys> > q; void dij(int s) { lys son; son.point=s; son.val=0; //vis[s]=1; memset(dis,0x7f,sizeof(dis)); dis[s]=0; q.push(son); while(!q.empty()) { lys son=q.top(); q.pop(); int x=son.point,e=son.val; if(vis[x]) { continue; } vis[x]=1; for(int i=head[x];i;i=edge[i].next) { if(dis[edge[i].to]>dis[x]+edge[i].val&&!vis[edge[i].to]) { dis[edge[i].to]=e+edge[i].val; son.point=edge[i].to; son.val=dis[edge[i].to]; q.push(son); //printf("%d %d\n",son.point,son.val); } } } } int main() { // freopen("lyslyslys.in","r",stdin); cin>>n>>m>>s; for(int i=1;i<=m;i++) { int a,b,c; cin>>a>>b>>c; add(a,b,c); } dij(s); for(int i=1;i<=n;i++) { printf("%d ",dis[i]); } }