Pagini recente » Cod sursa (job #2734352) | Cod sursa (job #398221) | Cod sursa (job #924558) | Cod sursa (job #3206638) | Cod sursa (job #3297439)
#include <bits/stdc++.h>
#define NMAX 50000
#define VMAX 2000000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, a, b, c;
vector<pair<int, int>> graf[NMAX + 2];
vector<int> dist(NMAX + 2, VMAX);
vector<bool> viz(NMAX + 2);
priority_queue<pair<int, int>> pq;
void Dijkstra(int start) {
dist[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
int cost = -pq.top().first;
int nod = pq.top().second;
pq.pop();
if (viz[nod]) {
continue;
}
viz[nod] = 1;
for (pair<int, int> it : graf[nod]) {
int vec = it.first;
int val = it.second;
if (cost + val < dist[vec]) {
dist[vec] = cost + val;
pq.push({-dist[vec], vec});
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> n >> m;
while (m--) {
fin >> a >> b >> c;
graf[a].push_back({b, c});
}
Dijkstra(1);
for (int i = 2; i <= n; i++) {
if (dist[i] == VMAX) {
fout << 0 << ' ';
}
else {
fout << dist[i] << ' ';
}
}
return 0;
}