Pagini recente » Cod sursa (job #2605587) | Cod sursa (job #602115) | Cod sursa (job #1813047) | Cod sursa (job #133361) | Cod sursa (job #1933072)
#include <bits/stdc++.h>
#define oo 2147483647
using namespace std;
vector< pair<int, int> > G[50005];
int d[50005];
bool viz[50005];
bool cmp(int a, int b) {
return d[a] > d[b];
}
priority_queue<int, vector<int>, function<bool(int, int)> > q(cmp);
int main()
{
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, x, y, cost;
f >> n >> m;
for(int i = 1; i <= m; i ++) {
f >> x >> y >> cost;
G[x].push_back({y, cost});
}
for(int i = 2; i <= n; i ++) d[i] = oo;
q.push(1);
viz[1] = true;
while(!q.empty()) {
int nod = q.top(); q.pop();
int cost = d[nod];
for(auto j: G[nod]) {
if(d[j.first] > cost + j.second)
d[j.first] = cost + j.second;
if(!viz[j.first]) {
viz[j.first] = true;
q.push(j.first);
}
}
}
for(int i = 2; i <= n; i ++) {
if(d[i] == oo) d[i] = 0;
g << d[i] << " ";
}
g << "\n";
return 0;
}