Pagini recente » Cod sursa (job #1832491) | Cod sursa (job #238669) | Cod sursa (job #2900987) | Cod sursa (job #1797356) | Cod sursa (job #3337295)
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct muchie{
int u, v;
int cost;
bool operator<(const muchie& other) const{
return cost < other.cost;
}
};
vector<vector<pair<int, int>>> adj; // {cost, vecin}
vector<bool> viz;
vector<int> d;
vector<int> tata;
void dijkstra(int s){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
tata[s] = 0;
d[s] = 0;
while(!pq.empty()){
int u = pq.top().second;
pq.pop();
if(viz[u] == false){
viz[u] = true;
for(auto perecheVecin : adj[u]){
int v = perecheVecin.second;
int cost = perecheVecin.first;
if(viz[v] == false && d[v] > d[u] + cost){
d[v] = d[u] + cost;
tata[v] = u;
pq.push({d[v], v});
}
}
}
}
}
const int INF = 1e9;
int main(){
int n, m;
fin >> n >> m;
adj.assign(n + 1, {});
tata.assign(n + 1, -1);
d.assign(n + 1, INF);
viz.assign(n + 1, false);
for(int i = 0; i < m; i++){
muchie mch;
fin >> mch.u >> mch.v >> mch.cost;
adj[mch.u].push_back({mch.cost, mch.v});
}
int s = 1;
dijkstra(s);
for(int i = 2; i <= n; i++)
if(d[i] == INF)
fout << 0 << " ";
else fout << d[i] << " ";
return 0;
}