Pagini recente » Cod sursa (job #1031158) | Cod sursa (job #1038336) | Robert Hangu | Cod sursa (job #3273341) | Cod sursa (job #3216399)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int NMAX = 50004, inf = 20000 * 50000;
int m, n;
bool fr[NMAX];
struct mu {
int to, c;
};
vector<mu> v[NMAX];
int main() {
f >> n >> m;
for(int i = 0; i < m; i++) {
int x, y, z;
f >> x >> y >> z;
v[x].push_back({y, z});
}
vector<int> d(NMAX, inf);
auto cmp = [](mu a, mu b) { return a.c > b.c; };
priority_queue<mu, vector<mu>, decltype(cmp)> q(cmp);
d[1] = 0;
q.push({1, 0});
while(!q.empty()) {
mu x = q.top();
q.pop();
if(!fr[x.to]) {
fr[x.to] = 1;
for (mu i : v[x.to])
if (d[i.to] > x.c + i.c) {
d[i.to] = x.c + i.c;
q.push({i.to, d[i.to]});
}
}
}
for(int i = 2; i <= n; i++)
g << d[i] << ' ';
}