Pagini recente » Cod sursa (job #2322382) | Cod sursa (job #1000575) | Cod sursa (job #2958273) | Cod sursa (job #1425342) | Cod sursa (job #2722215)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define endl "\n"
const int Nmax = 50100;
const int MODULO = 1e9 + 7;
const int oo = -1e9;
int n, m;
int dist[Nmax];
bool viz[Nmax];
struct compara {
bool operator()(int x, int y) {
return dist[x] > dist[y];
}
};
vector < pair < int , int > > g[Nmax];
priority_queue < int , vector < int > , compara > pq;
void solve() {
cin >> n >> m;
while(m--) {
int x, y, c; cin >> x >> y >> c;
g[x].push_back({y, c});
}
for(int i = 1; i <= n; i++) {
dist[i] = INT_MAX / 2 - 1;
}
dist[1] = 0;
viz[1] = 1;
pq.push(1);
while(!pq.empty()) {
int nod = pq.top(); pq.pop();
viz[nod] = false;
for(auto it : g[nod]) {
int vecin = it.first;
int cost = it.second;
if(dist[nod] + cost < dist[vecin]) {
dist[vecin] = dist[nod] + cost;
if(viz[vecin] == false) {
pq.push(vecin);
viz[vecin] = true;
}
}
}
}
for(int i = 2; i <= n; i++) {
if(dist[i] != INT_MAX / 2 - 1) {
cout << dist[i] << " ";
} else {
cout << 0 << " ";
}
}
}
void fisier() {
freopen("dijkstra.in" , "r", stdin);
freopen("dijkstra.out", "w", stdout);
}
int main() {
ios_base::sync_with_stdio(0);
cin .tie(0);
cout.tie(0);
fisier();
int testCases = 1;
//cin >> testCases;
while(testCases--) {
solve();
}
}