Pagini recente » Cod sursa (job #1681116) | Cod sursa (job #2482149) | Cod sursa (job #297253) | Cod sursa (job #2374662) | Cod sursa (job #3220037)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int OO = (1 << 30);
const int NMax = 50005;
struct Node{
int index, cost;
Node(int index, int cost): index(index), cost(cost){}
bool operator<(const Node& other) const{
return this->cost > other.cost;
}
};
priority_queue<Node> q;
vector<pair<int, int>> G[NMax];
int d[NMax], n;
void dijsktra(){
for(int i=1; i<=n; ++i){
d[i] = OO;
}
d[1] = 0;
q.emplace(1, 0);
while(!q.empty()){
int nod = q.top().index, ct = q.top().cost;
if(ct == d[nod]){
for(int i=0; i<G[nod].size(); ++i){
int vecin = G[nod][i].first, costVecin = G[nod][i].second;
if(d[nod] + costVecin < d[vecin]){
d[vecin] = d[nod] + costVecin;
q.emplace(vecin, d[vecin]);
}
}
}
q.pop();
}
for(int i=1; i<=n; ++i){
if(d[i] == OO){
d[i] = 0;
}
}
}
int main()
{
int m;
fin >> n >> m;
for(int x, y, ct, i=1; i<=m; ++i){
fin >> x >> y >> ct;
G[x].push_back(make_pair(y, ct));
}
dijsktra();
for(int i=2; i<=n; ++i){
fout << d[i] << ' ';
}
fout << '\n';
return 0;
}