Pagini recente » Cod sursa (job #1546080) | Cod sursa (job #2832854) | Cod sursa (job #1640359) | Cod sursa (job #236343) | Cod sursa (job #3337069)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
constexpr int INF = 1e9;
int main(){
int n,m;
fin >> n>>m;
vector<vector<pair<int,int>>> lista(n+1);
// vector<pair<int,int>> rez;
vector<bool> viz(n+1, false);
vector<int> dist(n+1, INF);
for (int i = 0; i<m; i++) {
int u,v,c;
fin>>u>>v>>c;
lista[u].emplace_back(v,c);
}
priority_queue< tuple<int,int,int>, vector<tuple<int,int,int>>, greater<>> pq; // cost, u,v
pq.emplace(0,1,-1);
// viz[1] = true;
dist[1] = 0;
while (!pq.empty()) {
auto [cost,u,tata] = pq.top();
pq.pop();
if (viz[u]) continue;
// if (cost > dist[u]) continue;
viz[u] = true;
for (auto [v,c] : lista[u]) {
if (!viz[v] && dist[v] > dist[u] + c) {
dist[v] = dist[u]+c;
pq.emplace(dist[v],v,u);
}
}
}
for (int i = 2; i<=n ;i ++) {
fout<<dist[i]<<" ";
}
return 0;
}