Pagini recente » Cod sursa (job #1576993) | Cod sursa (job #2031886) | Cod sursa (job #1916545) | Cod sursa (job #816885) | Cod sursa (job #3350616)
#include <fstream>
#include <vector>
#include <queue>
std::ifstream fin("dijkstra.in");
std::ofstream fout("dijkstra.out");
typedef std::pair<int, int> pii;
const int INF = 1e9;
int N, M;
std::vector<std::vector<pii> > G;
std::priority_queue<pii> pq;
std::vector<int> D;
int main(){
fin >> N >> M;
G.resize(N + 1);
D.resize(N + 1, INF);
for(int x, y, c; M--;){
fin >> x >> y >> c;
G[x].push_back({y, c});
}
D[1] = 0;
pq.push({0, 1});
while(!pq.empty()){
int node = pq.top().second;
pq.pop();
for(auto [y, c] : G[node]){
if(D[y] > D[node] + c){
D[y] = D[node] + c;
pq.push({-D[y], y});
}
}
}
for(int i = 2; i <= N; ++i){
if(D[i] == INF){
fout << "0 ";
}
else{
fout << D[i] << ' ';
}
}
}