Pagini recente » Cod sursa (job #1331539) | Cod sursa (job #1020828) | Cod sursa (job #3153629) | Cod sursa (job #2321518) | Cod sursa (job #2370958)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream inf("dijkstra.in");
ofstream outf("dijkstra.out");
struct Drum {
int dest, cost;
};
vector<Drum> ad[50001];
int dist[50001];
bool viz[50001];
priority_queue<int, deque<int>, greater<int> > pq;
void dij(int x) {
pq.push(x);
memset(dist, 64, sizeof(dist));
dist[x] = 0;
viz[x] = 1;
while(!pq.empty()) {
x = pq.top();
pq.pop();
viz[x] = 0;
for(Drum& d : ad[x]) {
if(dist[d.dest] > dist[x] + d.cost) {
dist[d.dest] = dist[x] + d.cost;
if(viz[d.dest] == 0) {
pq.push(d.dest);
viz[d.dest] == 0;
}
}
}
}
}
int main() {
int n, m, x, y, l;
inf >> n >> m;
for(int i = 0; i < m; i++) {
inf >> x >> y >> l;
ad[x].push_back({y, l});
}
dij(1);
for(int i = 2; i <= n; i++) {
if(dist[i] == 1077952576) {
outf << "0 ";
}
else {
outf << dist[i] << ' ';
}
}
return 0;
}