Pagini recente » Cod sursa (job #636751) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #178489) | Cod sursa (job #3320437)
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int NMAX = 5e2;
struct node {
int node;
ll cost;
};
bool operator < (const node & a, const node & b) {
return a.cost > b.cost;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("D:\\cf2\\problema\\input.txt", "r", stdin);
freopen("D:\\cf2\\problema\\output.txt", "w", stdout);
#endif
int n, m, a, b, c;
cin >> n >> m;
vector<vector<node>> v(n + 1);
vector<ll> dist(n + 1, INF);
while (m--) {
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
priority_queue<node> pq;
pq.push({1, 0});
while (pq.size()) {
while (pq.size() && dist[pq.top().node] != INF) pq.pop();
if (pq.size()) {
node nod = pq.top();
pq.pop();
dist[nod.node] = nod.cost;
for (node i : v[nod.node]) {
if (dist[i.node] == INF) pq.push({i.node, i.cost + dist[nod.node]});
}
}
}
for (int i = 2; i <= n; i++) cout << dist[i] << ' ';
return 0;
}