Mai intai trebuie sa te autentifici.
Cod sursa(job #2628461)
Utilizator | Data | 16 iunie 2020 02:06:07 | |
---|---|---|---|
Problema | Algoritmul lui Dijkstra | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.7 kb |
#include <bits/stdc++.h>
using namespace std;
struct H {
int n,d;
bool operator<(const H&x)const{return d>x.d;}
};
int D[50001],n,m,i,j,c,I=INT_MAX;
vector<H>v[50001];
main() {
ifstream f{"dijkstra.in"};
ofstream g{"dijkstra.out"};
f >> n >> m;
while(m--)
f >> i >> j >> c,
v[i].push_back({j,c});
fill(D+2,D+n+1,I);
priority_queue<H>q;
q.push({1,0});
while (q.size()) {
H h=q.top();
q.pop();
if (D[h.n] == h.d)
for (H t: v[h.n])
if (D[t.n] > h.d + t.d)
D[t.n] = h.d + t.d,
q.push({t.n, D[t.n]});
}
for (i=2; i<=n; ++i)
g << (D[i]^I?D[i]:0) << ' ';
}