Pagini recente » Cod sursa (job #171614) | Cod sursa (job #2034229) | Cod sursa (job #204578) | Cod sursa (job #578032) | Cod sursa (job #2173232)
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<cstring>
#define NMAX 50010
using namespace std;
typedef pair<int, int> int_pair;
vector<int_pair> G[NMAX];
int dist[NMAX];
void dijkstra(int N, int start) {
set<int_pair> tree;
memset(dist, -1, sizeof(dist));
dist[start] = 0;
tree.insert(make_pair(0, start));
int node;
while (!tree.empty()) {
auto it = tree.begin();
node = it->second;
tree.erase(it);
for (auto &edge : G[node]) {
if (dist[edge.first] == - 1 || dist[edge.first] > dist[node] + edge.second) {
if (dist[edge.first] != -1) {
tree.erase(make_pair(dist[edge.first], edge.first));
}
dist[edge.first] = dist[node] + edge.second;
tree.insert(make_pair(dist[edge.first], edge.first));
}
}
}
}
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
int N, M, x, y, c, i;
cin >> N >> M;
for (i = 0; i < M; i++) {
cin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
dijkstra(N, 1);
for (i = 2; i <= N; i++) {
cout << ((dist[i] == -1) ? 0 : dist[i]) << ' ';
}
return 0;
}