Pagini recente » Cod sursa (job #2112221) | Cod sursa (job #2624808) | Cod sursa (job #1611650) | Cod sursa (job #2219873) | Cod sursa (job #2243077)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 50004;
struct at {
int fi, se;
const bool operator < (const at &a) const {
return fi > a.fi;
}
};
int n, m;
vector <at > v[maxn];
int d[maxn];
priority_queue <at> pq;
const int maxnr = 0x3f3f3f3f;
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1;i <= m;i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
v[a].push_back({b, c});
}
memset(d, 0x3f3f3f3f, sizeof d);
pq.push({0, 1});
while(!pq.empty()) {
at nod = pq.top();
pq.pop();
int cst = nod.fi, nd = nod.se;
if(d[nd] == maxnr) {
d[nd] = cst;
for(auto &x : v[nd]) {
if(d[x.fi] == maxnr) {
pq.push({cst + x.se, x.fi});
}
}
}
}
for(int i = 2;i <= n;i++)
if(d[i] != maxnr)
cout << d[i] << " ";
else
cout << "0 ";
return 0;
}