Pagini recente » Cod sursa (job #2721681) | Cod sursa (job #2639360) | Cod sursa (job #1078113) | Cod sursa (job #2946330) | Cod sursa (job #2638977)
#include <bits/stdc++.h>
using namespace std;
const int kNmax = 50005;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct Compare {
bool operator()(pair<int, int> p1, pair<int, int> p2) {
return p1.second > p2.second;
}
};
int main() {
int n, m;
vector<pair<int, int> > adj[kNmax];
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, c;
fin >> a >> b >> c;
adj[a].push_back({b, c});
}
vector<int> d(n + 1, numeric_limits<int>::max());
vector<bool> selected(n + 1, false);
priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq;
d[1] = 0;
pq.push({1, 0});
while (!pq.empty()) {
pair<int, int> tmp = pq.top();
pq.pop();
if (selected[tmp.first] == false) {
for (auto node : adj[tmp.first]) {
if (d[node.first] > d[tmp.first] + node.second) {
d[node.first] = d[tmp.first] + node.second;
pq.push({node.first, d[node.first]});
}
}
selected[tmp.first] = true;
}
}
for (int i = 1; i <= n; ++i) {
if (d[i] == numeric_limits<int>::max()) {
d[i] = 0;
}
}
for (int i = 2; i < d.size(); ++i) {
fout << d[i] << " ";
}
return 0;
}