Pagini recente » Borderou de evaluare (job #1557629) | Borderou de evaluare (job #2448273) | Borderou de evaluare (job #319811) | Borderou de evaluare (job #385092) | Cod sursa (job #3181078)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <unordered_set>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <climits>
#include <random>
#include <sstream>
#include <bitset>
#include <stack>
#include <functional>
#include <chrono>
#include <complex>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int INF = 1e9;
vector<pair<int, int>> L[100005];
int n, m;
void dijkstra(int s, vector<int>& d, vector<int>& p) {
d.assign(n + 1, INF);
p.assign(n + 1, -1);
d[s] = 0;
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push({ 0, s });
while (!q.empty()) {
int v = q.top().second;
int d_v = q.top().first;
q.pop();
if (d_v != d[v])
continue;
for (auto edge : L[v]) {
int to = edge.first;
int len = edge.second;
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
p[to] = v;
q.push({ d[to], to });
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
cin >> x >> y >> c;
L[x].emplace_back(y, c);
}
vector<int> dist, par;
dijkstra(1, dist, par);
for (int i = 2; i <= n; i++)
{
if (dist[i] == INF)
cout << 0 << ' ';
else
cout << dist[i] << ' ';
}
cout << '\n';
return 0;
}