Pagini recente » Cod sursa (job #2879571) | Cod sursa (job #699435) | Cod sursa (job #1075800) | Cod sursa (job #2883248) | Cod sursa (job #3199896)
//#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int oo=(1<<30);
struct pereche
{
int cost, nod;
//bool operator<(pereche b)
//{
// return cost>b.cost;
//}
};
auto cmp=[](pereche a, pereche b){ return a.cost > b.cost; };
priority_queue<pereche, vector<pereche>, decltype(cmp)> Q(cmp);
int N,M;
vector<pereche> G[50005];
int dist[50005];
bool viz[50005];
void dijkstra(int st)
{
for(int i=1;i<=N;i++)
dist[i]=oo,viz[i]=false;
dist[1]=0;
Q.push({0, 1});
while(!Q.empty())
{
pereche p = Q.top();Q.pop();
if(viz[p.nod])
continue;
viz[p.nod] = true;
for(auto&i:G[p.nod])
{
if(dist[p.nod]+i.cost<dist[i.nod])
{
dist[i.nod]=dist[p.nod]+i.cost;
if(viz[i.nod]==false)
{
Q.push({dist[i.nod],i.nod});
}
}
}
}
}
int main()
{
cin>>N>>M;
for(int i=0;i<M;i++)
{
int x,y,c;
cin>>x>>y>>c;
G[x].push_back({c,y});
}
dijkstra(1);
for(int i=2;i<=N;i++)
if(dist[i]==oo)
cout<<0<<" ";
else
cout<<dist[i]<<" ";
return 0;
}