Nu aveti permisiuni pentru a descarca fisierul grader_test4.in
Cod sursa(job #2729879)
| Utilizator | Data | 25 martie 2021 15:39:10 | |
|---|---|---|---|
| Problema | Algoritmul lui Dijkstra | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.01 kb |
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream in ("dijkstra.in");
ofstream out ("dijkstra.out");
const int N = 50001;
const int INF = 1e9 + 15;
int n;
int m;
vector<pair<int, int>> a[N];
priority_queue<pair<int, int>> pq;
bitset<N> prelucrat;
int d[N];
void citire() {
in >> n >> m;
for (int i = 0; i < m; i++) {
int x;
int y;
int c;
in >> x >> y >> c;
a[x].push_back({y, c});
}
in.close();
return;
}
int main() {
citire();
for (int i = 2; i <= n; i++) {
d[i] = INF;
}
d[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
if (prelucrat[x]) {
continue;
}
prelucrat[x] = 1;
for (auto p : a[x]) {
int y = p.first;
int c = p.second;
if (d[x] + c < d[y]) {
d[y] = d[x] + c;
pq.push({-d[y], y});
}
}
}
for (int i = 2; i <= n; i++) {
if (d[i] == INF) {
d[i] = 0;
}
out << d[i] << " ";
}
out.close();
return 0;
}