Pagini recente » Cod sursa (job #2571276) | Cod sursa (job #2121279) | Cod sursa (job #2219698) | Cod sursa (job #276780) | Cod sursa (job #2207566)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int inf = 1e9;
vector< vector< pair< int, int > > > G;
vector< int > dist;
void Dijkstra(int nod) {
priority_queue< pair< int, int > > pq;
pq.push({0, nod});
dist[nod] = 0;
while(!pq.empty()) {
int tempNode = pq.top().second;
int tempDist = -pq.top().first; pq.pop();
if(tempDist > dist[tempNode]) {
continue;
}
for(auto vecin: G[tempNode]) {
if(dist[tempNode] + vecin.second < dist[vecin.first]) {
dist[vecin.first] = dist[tempNode] + vecin.second;
pq.push({-dist[vecin.first], vecin.first});
}
}
}
}
int main() {
ios::sync_with_stdio(false); in.tie(0); out.tie(0);
int n, m; in >> n >> m;
G.resize(n); dist.resize(n, inf);
for(int i = 1; i <= m; ++i) {
int x, y, cost; in >> x >> y >> cost;
--x; --y;
G[x].push_back({y, cost});
G[y].push_back({x, cost});
}
Dijkstra(0);
for(int i = 1; i < n; ++i) {
if(dist[i] != inf) {
out << dist[i] << " ";
} else {
out << "0 ";
}
}
in.close(); out.close();
return 0;
}