Pagini recente » Cod sursa (job #1333019) | Cod sursa (job #3212418) | Cod sursa (job #1751844) | Cod sursa (job #1933760) | Cod sursa (job #2843219)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
vector<pair<int, int>> graf[50005];
int dist[50005];
bool visited[50005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, d;
cin >> x >> y >> d;
graf[x].push_back({ y, d });
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({ 0, 1 });
while (!q.empty()) {
int now = q.top().second;
visited[now] = true;
for (auto i : graf[now]) {
if (!visited[i.first]) {
dist[i.first] = dist[now] + i.second;
q.push({ dist[i.first], i.first });
}
}
q.pop();
}
for (int i = 2; i <= n; i++) {
cout << dist[i] << ' ';
}
return 0;
}