Pagini recente » Cod sursa (job #1329448) | Cod sursa (job #1818091) | Cod sursa (job #2445089) | Cod sursa (job #2926090) | Cod sursa (job #2046074)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
#define pii pair<int, int>
const int inf = 2e9;
const int N = 50010;
class cmp{
public:
bool operator()( pii & a, pii & b){
return a.second < b.second;
}
};
priority_queue<pii, vector< pii >, cmp> H;
int dist[N];
vector< pii > gr[N];
int main(){
int n, m;
cin >> n >> m;
for(int i = 0; i < m; ++i){
int a, b, c;
cin >> a >> b >> c;
gr[a].push_back({b, c});
}
for(int i = 2; i <= n; ++i) dist[i] = inf;
H.push({1, 0});
while(H.size()){
int node = H.top().first;
int cost = H.top().second;
H.pop();
if(cost != dist[node]) continue;
for(auto x : gr[node]){
if(cost + x.second < dist[x.first]){
dist[x.first] = cost + x.second;
H.push({x.first, dist[x.first]});
}
}
}
for(int i = 2; i <= n; ++i){
if(dist[i] == inf) cout << "0 ";
else cout << dist[i] << ' ';
}
return 0;
}