Pagini recente » Cod sursa (job #2914609) | Cod sursa (job #2478098) | Cod sursa (job #1598800) | Cod sursa (job #1312861) | Cod sursa (job #2858861)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <fstream>
#include <cassert>
#include <vector>
#include <set>
using namespace std;
const int NMAX = 50005;
const int INF = 1000001;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<pair<int, int>> graph[NMAX];
int dist[NMAX];
int main() {
int n, m;
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int start, end, cost;
fin >> start >> end >> cost;
graph[start].push_back(make_pair(end, cost));
}
for (int i = 2; i <= n; ++i) {
dist[i] = INF;
}
dist[1] = 0;
set<pair<int, int>> h;
h.insert(make_pair(0, 1));
while (!h.empty()) {
int node = h.begin()->second;
h.erase(h.begin());
for (pair<int, int> next : graph[node]) {
int to = next.first;
int cost = next.second;
if (dist[to] > dist[node] + cost) {
if (dist[to] != INF) {
h.erase(h.find(make_pair(dist[to], to)));
}
dist[to] = dist[node] + cost;
h.insert(make_pair(dist[to], to));
}
}
}
for (int i = 2; i <= n; ++i) {
if (dist[i] == INF) {
dist[i] = 0;
}
fout << dist[i] << ' ';
}
}