Pagini recente » Cod sursa (job #2941920) | Cod sursa (job #1885813) | Cod sursa (job #2593069) | Cod sursa (job #1814423) | Cod sursa (job #1823987)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int nmax = 5e4+5;
const int oo = (1<<30);
vector <pair<int,int> > g[nmax];
int dist[nmax], n;
class cmp {
public:
inline bool operator()(const int &x, const int &y) {
return dist[x] > dist[y];
}
};
void dijkstra(int start) {
priority_queue <int, vector<int>, cmp> heap;
int dad, son, cost, i;
for(i=1; i<=n; i++) {
dist[i]=oo;
}
dist[start]=0;
heap.push(start);
while(!heap.empty()) {
dad=heap.top();
heap.pop();
for(auto it : g[dad]) {
son=it.first;
cost=it.second;
if(dist[son] > dist[dad]+cost) {
dist[son]=dist[dad]+cost;
heap.push(son);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
int m, i, x, y, c;
fin >> n >> m;
for(i=1; i<=m; i++) {
fin >> x >> y >> c;
g[x].push_back({y, c});
}
dijkstra(1);
for(i=2; i<=n; i++) {
if(dist[i]==oo) dist[i]=0;
fout << dist[i] << " ";
}
fin.close();
fout.close();
return 0;
}