Pagini recente » Cod sursa (job #1842360) | Cod sursa (job #2856893) | Cod sursa (job #1863780) | Cod sursa (job #576575) | Cod sursa (job #2944779)
//
// Created by Radu Buzas on 22.11.2022.
//
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
struct cost{
int c, x;
bool operator<(const cost & o) const{
return (this->c > o.c);
}
};
int n, m;
vector<vector<cost>> graph;
vector<int> d;
void dijkstra(int x){
d[x] = 0;
priority_queue<cost> q;
struct cost S;
S.c = 0; S.x = x;
q.push(S);
while (!q.empty()){
int cost = q.top().c;
int nod = q.top().x;
q.pop();
for (auto k : graph[nod]) {
if(cost + k.c < d[k.x]){
d[k.x] = cost + k.c;
S.c = cost + k.c;
S.x = k.x;
q.push(S);
}
}
}
}
int main(){
in >> n >> m;
graph.resize(n+1);
d.resize(n+1, ((unsigned)1 << 31) -1);
for(int i = 1; i <= m; ++i){
int x;
struct cost S;
in >> x >> S.x >> S.c;
graph[x].push_back(S);
// graph[y].push_back({x, c});
}
dijkstra(1);
for (int i = 2; i <= n; ++i) {
if(d[i] == ((unsigned)1 << 31) -1)
out << 0 << ' ';
out << d[i] << ' ';
}
in.close();
out.close();
return 0;
}