dijstra
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=10010,MAXM=500010;
int inf=2147483647;
struct XY{
int w,to,pre;
}e[MAXM];
struct XX{
int dis,num;
}d[MAXN],tmp;
struct cmp1{
bool operator ()(XX &a,XX &b){
return a.dis>b.dis;
}
};
int n,m,s,sz=0;
int las[100010];
bool flag[MAXN];
priority_queue<XX,vector<XX>,cmp1> q;
void add(int x,int y,int w){
++sz;e[sz].to=y;e[sz].w=w;e[sz].pre=las[x];las[x]=sz;
}
void Dijkstra(){
int min,u=0;
d[s].dis=0;q.push(d[s]);
while (!q.empty()){
u=q.top().num;q.pop();
if (flag[u]) continue;
flag[u]=true;
for (int j=las[u];j;j=e[j].pre){
int mu=e[j].to;
if (d[mu].dis>d[u].dis+e[j].w){
d[mu].dis=d[u].dis+e[j].w;
q.push(d[mu]);
}
}
}
}
int main(){
int xx,yy,zz;
cin >>n>>m>>s;
for (int i=1;i<=n;++i){
d[i].num=i;d[i].dis=inf;
}
for (int i=1;i<=m;++i){
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);
}
Dijkstra();
for (int i=1;i<=n;++i)
printf("%d ",d[i].dis);
cout <<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define maxn 10005
#define maxm 500005
#define INF 1234567890
inline int read()
{
int x=0,k=1; char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')k=-1;c=getchar();}
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*k;
}
struct Edge
{
int u,v,w,next;
}e[maxm];
int head[maxn],cnt,n,m,s,vis[maxn],dis[maxn],pre[maxn];
struct node
{
int w,now;
inline bool operator <(const node &x)const
{
return w>x.w;
}
};
priority_queue<node>q;
inline void add(int u,int v,int w)
{
e[++cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
void print(int x)
{
if(pre[x]==0)return ;
print(pre[x]);
cout<<"-> "<<x;
}
void dijkstra()
{
for(int i=1;i<=n;i++)
{
dis[i]=INF;
}
dis[s]=0;
q.push((node){
0,s});
while(!q.empty())
{
node x=q.top();
q.pop();
int u=x.now;
if(vis[u]) continue;
vis[u]=1;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(dis[v]>dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
pre[v]=u;
q.push((node){
dis[v],v});
}
}
}
}
int main()
{
n=read(),m=read(),s=read();
for(int i=1,x,y,z;i<=m;i++)
{
x=read(),y=read(),z=read();
add(x,y,z);
}
dijkstra();
for(int i=1;i<=n;i++)
{
printf("%d ",dis[i]);
}
return 0;
}
SPFA
void SPFA(int x)
{
d[x]=0;
for(int i=1;i<=n;i++)
d[i]=+OO;
queue<int>Q;
Q.push(x);
inq[x]=true;
while(!Q.empty())
{
int k=Q.front;Q.pop();inq[k]=false;
for(int i=head[k];i!=0;i=edge[i].next)
{
int j=edge[i].u ;
if(d[j]>d[k]+edge[i].w )
{
d[j]=d[k]+edge[i].w;
if(inq[j]!){
Q.push(j);
inq[k]=true;
}
}
}
}
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
const long long inf=2147483647;
const int maxn=10005;
const int maxm=500005;
using namespace std;
int n,m,s,num_edge=0;
int dis[maxn],vis[maxn],head[maxm];
struct Edge
{
int next,to,dis;
}edge[maxm];
void addedge(int from,int to,int dis)
{
edge[++num_edge].next=head[from];
edge[num_edge].to=to;
edge[num_edge].dis=dis;
head[from]=num_edge;
}
void spfa()
{
queue<int> q;
for(int i=1; i<=n; i++)
{
dis[i]=inf;
vis[i]=0;
}
q.push(s); dis[s]=0; vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop(); vis[u]=0;
for(int i=head[u]; i; i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].dis)
{
dis[v]=dis[u]+edge[i].dis;
if(vis[v]==0)
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
cin>>m>>n;
s=n;
for(int i=1; i<=m; i++)
{
int f,g,w;
cin>>f>>g>>w;
addedge(f,g,w);
addedge(g,f,w);
}
spfa();
cout<<dis[1]<<endl;
return 0;
}