Pagini recente » Cod sursa (job #1430146) | Cod sursa (job #3323215) | Cod sursa (job #1516656) | Cod sursa (job #1580967) | Cod sursa (job #3325337)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 5e5 + 1;
const int INF = 1e9;
vector< pair<int, int> > adj[nmax];
int d[nmax], vis[nmax];
priority_queue < pair<int, int> > pq;
int main() {
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; i ++) {
int x, y, c;
fin >> x >> y >> c;
adj[x].push_back({y, c});
}
for(int i = 1; i <= n; i ++) {
d[i] = INF;
}
d[1] = 0;
pq.push({-d[1], 1});
while(!pq.empty()) {
int nod = pq.top().second;
pq.pop();
if(vis[nod] == 1) {
continue;
}
vis[nod] = 1;
for(auto& v : adj[nod]) {
int vecin = v.first;
int cost = v.second;
if(d[vecin] > d[nod] + cost) {
d[vecin] = d[nod] + cost;
pq.push({-d[vecin], vecin});
}
}
}
for(int i = 2; i <= n; i ++) {
fout << d[i] << ' ';
}
return 0;
}