Pagini recente » Cod sursa (job #252779) | Cod sursa (job #2811324) | Cod sursa (job #794968) | Cod sursa (job #2630949) | Cod sursa (job #1933060)
#include <bits/stdc++.h>
#define oo 2147483647
using namespace std;
vector< pair<int, int> > G[50005];
bool viz[50005];
int d[50005];
int main()
{
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, x, y, cost;
f >> n >> m;
for(int i = 1; i <= n; i ++) {
f >> x >> y >> cost;
G[x].push_back({y, cost});
}
for(int i = 2; i <= n; i ++) d[i] = oo;
for(int i = 1; i < n; i ++) {
int nod = 1, mn = oo;
for(int j = 1; j <= n; j ++) {
if(!viz[j] && d[j] < mn)
mn = d[j], nod = j;
}
viz[nod] = true;
for(auto j: G[nod]) {
if(d[j.first] > d[nod] + j.second) {
d[j.first] = d[nod] + j.second;
}
}
}
for(int i = 2; i <= n; i ++) {
if(d[i] == oo) d[i] = 0;
g << d[i] << " ";
}
g << "\n";
return 0;
}