Pagini recente » Borderou de evaluare (job #141036) | Cod sursa (job #1799437) | Cod sursa (job #2940769) | Cod sursa (job #1286754) | Cod sursa (job #2176414)
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 5e4, kMaxM = 2.5e5, kInf = 0x3f3f3f3f;
struct Edge {
int from, to;
int cost;
};
Edge e[kMaxM];
int start[kMaxN + 1];
int idx[kMaxM];
int dq[1 << 16];
int d[kMaxN], node_type[kMaxN];
int n;
void Pape(int s) {
for (int i = 0; i < n; ++i) {
d[i] = kInf;
node_type[i] = 2;
}
int q_start = 0, q_end = 1;
dq[0] = s;
d[s] = 0;
while (q_start != q_end) {
const int node = dq[(q_start++) & 65535];
node_type[node] = 0;
for (int i = start[node]; i != start[node + 1]; ++i) {
const int adj = e[idx[i]].to, cost = d[node] + e[idx[i]].cost;
if (d[adj] > cost) {
d[adj] = cost;
if (node_type[adj] == 1) {
continue;
}
const int where = (node_type[adj] == 2) ? q_end++ : ((--q_start & 65535) + 65536);
node_type[adj] = 1;
dq[where & 65535] = adj;
}
}
}
}
int main() {
#ifdef INFOARENA
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
#endif
cin.tie(0);
ios_base::sync_with_stdio(false);
int m; cin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y, c; cin >> x >> y >> c; --x; --y;
e[i] = {x, y, c};
}
for (int i = 0; i < m; ++i) {
++start[e[i].from];
}
for (int i = 1; i <= n; ++i) {
start[i] += start[i - 1];
}
for (int i = 0; i < m; ++i) {
idx[--start[e[i].from]] = i;
}
Pape(0);
for (int i = 1; i < n; ++i) {
if (d[i] == kInf) {
d[i] = 0;
}
cout << d[i] << " \n"[i == n - 1];
}
return 0;
}