Pagini recente » Cod sursa (job #1670474) | Cod sursa (job #2134210) | Cod sursa (job #2219349) | Cod sursa (job #2387909) | Cod sursa (job #2698345)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int main()
{
int n, m, x, y, c, s;
fin>>n>>m;
int d[n+1], t[n+1];
vector <pair <int, int>> v[n+1];
for(int i=1; i<=m; ++i)
{
fin>> x >> y >> c;
v[x].push_back({y, c});
}
for(int i=1; i<=n; ++i)
{
d[i]=INT_MAX;
t[i]=0;
}
priority_queue < pair <int, int>> q;
s=1;
d[s]=0;
q.push({d[s],s});
while(!q.empty())
{
x = q.top().second;
q.pop();
for(auto p: v[x])
{
y = p.first;
if( d[x] + p.second < d[y] )
{
d[y] = d[x] + p.second;
t[y] = x;
q.push({d[y], y});
}
}
}
for(int i = 2; i<=n; ++i) fout << d[i]<<' ';
return 0;
}