Pagini recente » Cod sursa (job #2954664) | Cod sursa (job #182387) | Solutii preONI 2006, Runda a 4-a | Cod sursa (job #2841931) | Cod sursa (job #2670791)
#include <bits/stdc++.h>
#define MAX 100005
#define oo (1 << 30)
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int n, m, d[MAX], x, y, cost;
vector < pair < int , int > > g[MAX];
struct cmp{
bool operator()(int a, int b){
return d[a] > d[b];
}
};
priority_queue < int , vector < int > , cmp > QAnon;
void Dijkstra(int k){
for(int i = 1; i <= n; i++)
d[i] = oo;
d[k] = 0;
QAnon.push(k);
while(!QAnon.empty()){
k = QAnon.top();
for(int i = 0; i < g[k].size(); i++){
int vec = g[k][i].first;
cost = g[k][i].second;
if(d[vec] > d[k] + cost){
d[vec] = d[k] + cost;
QAnon.push(vec);
}
}
QAnon.pop();
}
}
int main(){
in>>n>>m;
for(int i = 1; i <= m; i++){
in>>x>>y>>cost;
g[x].push_back({y, cost});
g[y].push_back({x, cost});
}
Dijkstra(1);
for(int i = 2; i <= n; i++)
out<<(d[i] == oo ? -1 : d[i])<<" ";
}