Pagini recente » Cod sursa (job #542761) | Cod sursa (job #3244381) | Cod sursa (job #1422041) | Cod sursa (job #2881609) | Cod sursa (job #2370915)
#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];
struct Nod {
int nod, cost;
};
struct comp {
bool operator()(Nod a, Nod b) {
return a.cost > b.cost;
}
};
priority_queue<Nod, vector<Nod>, comp> pq;
void dij(int x) {
pq.push({x, 0});
memset(dist, 64, sizeof(dist));
dist[x] = 0;
while(!pq.empty()) {
x = pq.top().nod;
pq.pop();
for(Drum& d : ad[x]) {
if(dist[d.dest] > dist[x] + d.cost) {
dist[d.dest] = dist[x] + d.cost;
pq.push({d.dest, dist[d.dest]});
}
}
}
}
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;
}