Pagini recente » Cod sursa (job #681984) | Cod sursa (job #2880275) | Cod sursa (job #3032834) | Cod sursa (job #3150527) | Cod sursa (job #2556867)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int MAXN = 1 * 1e5 + 5;
const int INF = 1e8;
struct edge{
int dest, cost;
bool operator < (const edge & aux) const{
return cost > aux.cost;
}
};
int dist[MAXN];
vector <edge> g[MAXN];
priority_queue <edge> pq;
void dijkstra(){
pq.push({1, 0});
while(pq.size()){
int d = pq.top().dest;
int c = pq.top().cost;
pq.pop();
if(dist[d] == INF){
dist[d] = c;
for(auto x : g[d]){
pq.push({x.dest, dist[d] + x.cost});
}
}
}
}
int main()
{
int n, m; fin >> n >> m;
for(int i = 1; i <= n; ++i) dist[i] = INF;
for(int i = 1; i <= m; ++i){
int x, y, c; fin >> x >> y >> c;
g[x].push_back({y, c});
}
dijkstra();
for(int i = 2; i <= n; ++i) fout << dist[i] << " " ;
return 0;
}