Pagini recente » Cod sursa (job #1885387) | Cod sursa (job #237685) | Monitorul de evaluare | Cod sursa (job #1264094) | Cod sursa (job #3335907)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int N = 50007;
vector<pair<int, int>> a[N];
vector<int> dist(N, -1);
int n, m;
void calc_dist()
{
priority_queue< pair<int, int> > pq;
pq.push({0, 1});
while(!pq.empty())
{
int x = pq.top().second;
int c = -pq.top().first;
pq.pop();
cerr << x << "\n";
if(dist[x] == -1)
{
dist[x] = c;
for(auto pw : a[x])
{
int y = pw.second;
int d = pw.first;
if(dist[y] == -1)
pq.push({-(c + d), y});
}
}
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
a[x].push_back({c, y});
}
calc_dist();
for(int i = 2; i <= n; i++)
fout << dist[i] << " ";
return 0;
}