Pagini recente » Cod sursa (job #2804950) | Cod sursa (job #818102) | Cod sursa (job #2142116) | Cod sursa (job #2060807) | Cod sursa (job #1885801)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 50010
#define inf 1e9
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair<int, int> > G[MAX];
priority_queue <pair<int, int> > PQ;
int dist[MAX], viz[MAX];
int main()
{
int n, m, x, y, c, i, nod;
fin >> n >> m;
while(fin>>x>>y>>c)
{
G[x].push_back(make_pair(c, y));
}
for(i = 1 ; i <= n ; i++)
{
dist[i] = inf;
}
dist[1] = 0;
PQ.push(make_pair(0, 1));
while(PQ.size())
{
PQ.top();
pair<int,int> aux=PQ.top();
PQ.pop();
nod=aux.second;
if(viz[nod])
continue;
viz[nod]=1;
for(auto it:G[nod])
if(dist[nod]+it.first<dist[it.second])
{
dist[it.second]=dist[nod]+it.first;
PQ.push(make_pair(-dist[it.second],it.second));
}
}
for(i = 2 ; i <= n ; i++)
{
if(dist[i] == inf)
dist[i] = -1;
cout << dist[i] << " ";
}
fin.close();
fout.close();
return 0;
}