Pagini recente » Cod sursa (job #48317) | Cod sursa (job #459216) | Cod sursa (job #1740481) | Cod sursa (job #633278) | Cod sursa (job #3197721)
#include <bits/stdc++.h>
#define INF INT_MAX
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N,M;
vector<bool> usedNodes;
vector<int> costNode;
vector<unordered_map<int,int>> G;
int main() {
fin >> N >> M;
usedNodes.assign(N+1, false);
costNode.assign(N+1,INF);
G.resize(N+1);
for (int i = 1; i <= M; i++) {
int nodeA,nodeB,cost;
fin >> nodeA >> nodeB >> cost;
G[nodeA][nodeB] = cost;
G[nodeB][nodeA] = cost;
}
queue<int> nodes;
nodes.push(1);
costNode[1] = 0;
while (!nodes.empty()) {
usedNodes[nodes.front()] = true;
for (const pair<int,int>& edge : G[nodes.front()]) {
if (costNode[nodes.front()] + edge.second <= costNode[edge.first]) {
costNode[edge.first] = costNode[nodes.front()] + edge.second;
if (!usedNodes[edge.first]) {
nodes.push(edge.first);
}
}
}
nodes.pop();
}
for (int i = 2; i <= N; i++) {
if (costNode[i] == INF) {
fout << 0 << ' ';
} else {
fout << costNode[i] << ' ';
}
}
return 0;
}