This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define N_ (N * 4)
#define INF 0x3f3f3f3f3f3f3f3fLL
int *ej[N], *ew[N], eo[N], *ej_[N_], *ew_[N_], eo_[N_];
void append(int **ej, int **ew, int *eo, int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
long long *dd_; int iq[N_ + 1], pq[N_], cnt;
int lt(int i, int j) { return dd_[i] < dd_[j]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
void dijkstra(int **ej, int **ew, int *eo, long long *dd, int n, int s) {
int i, o;
dd_ = dd;
memset(dd, 0x3f, n * sizeof *dd);
memset(pq, 0, n * sizeof *pq), cnt = 0;
dd[s] = 0, pq_add_last(s);
while (cnt) {
i = pq_remove_first();
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
long long d = dd[i] + w;
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
}
int main() {
static long long dds[N], ddt[N], dd[N_];
int n, n_, m, s, t, u, v, i, j, w, o;
scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &u, &v), n_ = n * 4, s--, t--, u--, v--;
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
while (m--) {
scanf("%d%d%d", &i, &j, &w), i--, j--;
append(ej, ew, eo, i, j, w);
append(ej, ew, eo, j, i, w);
}
dijkstra(ej, ew, eo, dds, n, s);
dijkstra(ej, ew, eo, ddt, n, t);
for (i = 0; i < n_; i++) {
ej_[i] = (int *) malloc(2 * sizeof *ej_[i]);
ew_[i] = (int *) malloc(2 * sizeof *ew_[i]);
}
for (i = 0; i < n; i++) {
append(ej_, ew_, eo_, i << 2 | 0, i << 2 | 1, 0);
append(ej_, ew_, eo_, i << 2 | 0, i << 2 | 2, 0);
append(ej_, ew_, eo_, i << 2 | 1, i << 2 | 3, 0);
append(ej_, ew_, eo_, i << 2 | 2, i << 2 | 3, 0);
}
for (i = 0; i < n; i++)
for (o = eo[i]; o--; ) {
j = ej[i][o], w = ew[i][o];
append(ej_, ew_, eo_, i << 2 | 0, j << 2 | 0, w);
append(ej_, ew_, eo_, i << 2 | 3, j << 2 | 3, w);
if (dds[i] + w + ddt[j] == dds[t])
append(ej_, ew_, eo_, i << 2 | 1, j << 2 | 1, 0);
if (dds[j] + w + ddt[i] == dds[t])
append(ej_, ew_, eo_, i << 2 | 2, j << 2 | 2, 0);
}
dijkstra(ej_, ew_, eo_, dd, n_, u << 2 | 0);
printf("%lld\n", dd[v << 2 | 3]);
return 0;
}
Compilation message (stderr)
commuter_pass.c: In function 'append':
commuter_pass.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
commuter_pass.c: In function 'main':
commuter_pass.c:84:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &u, &v), n_ = n * 4, s--, t--, u--, v--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%d%d%d", &i, &j, &w), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |