Pagini recente » Cod sursa (job #1380155) | Cod sursa (job #647384) | Cod sursa (job #2624565) | Cod sursa (job #1968854) | Cod sursa (job #2551627)
#include <bits/stdc++.h>
#define nmax 50005
#define INF 1000000
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
vector <pair<int,int>> v[nmax];
int n,m,dist[nmax];
void dijkstra()
{
priority_queue <pair<int,int>> q;
bool viz[nmax]={0};
for(int i=1;i<=n;i++)
dist[i]=INF;
dist[1]=0;
q.push({0,1});
while(!q.empty())
{
int a=q.top().second;
q.pop();
if(!viz[a])
{
viz[a]=1;
for(auto u: v[a])
{
int b=u.first;
int w=u.second;
if(dist[a]+w<dist[b])
{
dist[b]=dist[a]+w;
q.push({-dist[b],b});
}
}
}
}
}
int main()
{
int i,j;
in>>n>>m;
for(i=1;i<=m;i++)
{
int x,y,w;
in>>x>>y>>w;
v[x].push_back({y,w});
}
/*for(i=1;i<=n;i++)
{
for(auto u:v[i])
cout<<u.first<<" ";
cout<<endl;
for(auto u:v[i])
cout<<u.second<<" ";
cout<<endl<<endl;
}*/
dijkstra();
for(i=2;i<=n;i++)
{
if(dist[i]==INF)
dist[i]=0;
out<<dist[i]<<" ";
}
return 0;
}