Pagini recente » Cod sursa (job #578301) | Cod sursa (job #1878067) | Cod sursa (job #831173) | Cod sursa (job #1950747) | Cod sursa (job #2535973)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int N = 50001;
const int M = 250001;
const int INF = 1e9;
int n, m, st, dr, nr, x, y, c, d[N], lst[N], vf[M], urm[M], cst[M], q[N+2], nrq[N];
bool sel[N];
priority_queue< pair<int, int> > h;
void adauga(int x, int y, int c){
vf[++nr] = y;
cst[nr] = c;
urm[nr] = lst[x];
lst[x] = nr;
}
void dijkstra(int x0)
{
for(int i=1; i<=n; i++){
d[i] = INF;
sel[i] = false;
}
d[x0] = 0;
h.push(make_pair(d[x0], x0));
while(!h.empty()){
while(!h.empty() && sel[h.top().second])
h.pop();
if(h.empty())
return;
x = h.top().second;
sel[x] = true;
for(int p = lst[x]; p != 0; p = urm[p]){
y = vf[p];
c = cst[p];
if(d[x] + c < d[y]){
d[y] = d[x] + c;
h.push(make_pair(-d[y], y));
}
}
}
}
int main()
{
in>>n>>m;
for(int i=1; i<=m; i++){
in>>x>>y>>c;
adauga(x, y, c);
}
dijkstra(1);
for(int i=2; i<=n; i++){
if(d[i] == INF) out<<"0 ";
else out<<d[i]<<' ';
}
return 0;
}