Pagini recente » Cod sursa (job #2835758) | Cod sursa (job #1715555) | Cod sursa (job #412808) | Cod sursa (job #2555873) | Cod sursa (job #2595467)
#include <bits/stdc++.h>
#define N 50001
#define INF 0x3F3F3F3F
using namespace std;
vector <pair <int, int>> G[N];
array <int, N> dist;
int main () {
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
ios::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
int n, m, i, j, c;
fin >> n >> m;
for (; m; m--) {
fin >> i >> j >> c;
G[i].push_back(make_pair(j, c));
}
auto cmp = [&] (const pair <int, int> &a, const pair <int, int> &b) {
return a.second>b.second;
};
priority_queue <pair <int, int>, vector <pair <int, int>>, decltype(cmp)> PQ(cmp);
dist.fill(INF);
dist[1]=0;
PQ.push(make_pair(1, 0));
while (!PQ.empty()) {
auto save=PQ.top();
PQ.pop();
if (dist[save.first]==save.second)
for (auto it: G[save.first])
if (dist[it.first]>save.second+it.second)
dist[it.first]=save.second+it.second,
PQ.push(make_pair(it.first, dist[it.first]));
}
for (i=2; i<=n; i++)
if (dist[i]==INF)
fout << "0 ";
else
fout << dist[i] << ' ';
return 0;
}