Pagini recente » Cod sursa (job #2041273) | Cod sursa (job #2334195) | Istoria paginii runda/concurs_000002 | Cod sursa (job #808020) | Cod sursa (job #2568602)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
# define inf 0x3f3f3f3f
vector<vector<pair<int, int>>> G(100001);
int D[100001], n, m, nod;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
void dijkstra(int nod){
Q.push({0, nod});
for(int i = 1; i <= n; ++i)
D[i] = inf;
D[nod] = 0;
while(!Q.empty()){
int nod = Q.top().second;
int cost = Q.top().first;
if(D[nod] != cost)
continue;
for(auto x:G[nod])
if(D[x.first] > D[nod] + x.second)
D[x.first] = D[nod] + x.second, Q.push({D[x.first], x.first});
Q.pop();
}
for(int i = 2; i <= n; ++i)
cout << D[i] << ' ';
}
int main(){
int x, y, c;
cin >> n >> m;
nod = 1;
for(int i = 1; i <= m; ++i){
cin >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, c});
}
dijkstra(nod);
return 0;
}