Pagini recente » Cod sursa (job #456276) | Cod sursa (job #369125) | Cod sursa (job #536047) | Cod sursa (job #1082722) | Cod sursa (job #2588592)
#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));
}
priority_queue <pair <int, int>, vector <pair <int, int>>, greater<pair <int, int>>> PQ;
dist.fill(INF);
dist[1]=0;
PQ.push(make_pair(0, 1));
while (!PQ.empty()) {
auto save=PQ.top();
PQ.pop();
if (dist[save.second]==save.first)
for (auto it: G[save.second])
if (dist[it.first]>save.first+it.second)
dist[it.first]=save.first+it.second,
PQ.push(make_pair(dist[it.first], it.first));
}
for (i=2; i<=n; i++)
if (dist[i]==INF)
fout << "0 ";
else
fout << dist[i] << ' ';
return 0;
}