Pagini recente » Cod sursa (job #515216) | Cod sursa (job #663484) | Cod sursa (job #272732) | Cod sursa (job #1670928) | Cod sursa (job #3165067)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50003, INF = 2e9;
vector<pair<int, int> > g[NMAX]; // cost, nod
int d[NMAX], n;
void dijkstra(int s) {
for (int i = 1; i <= n; i++)
d[i] = INF;
d[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
pq.push(make_pair(0, s));
while (!pq.empty()) {
int curr = pq.top().second;
pq.pop();
for (auto nxt : g[curr]) {
int nod = nxt.first, cost = nxt.second;
if (d[curr] + cost < d[nod]) {
d[nod] = d[curr] + cost;
pq.push(make_pair( d[nod], nod));
}
}
}
}
int main() {
int m;
fin >> n >> m;
while (m--) {
int x, y, c;
fin >> x >> y >> c;
g[x].push_back(make_pair(y, c));
}
dijkstra(1);
for (int i = 2; i <= n; i++)
fout << d[i] << ' ';
return 0;
}