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 M 100000
int *ej[N], *ej_[N], eo[N], eo_[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int main() {
static int dd[N], qu1[N], qu2[N];
static char adj[N];
int n, m, cnt1, cnt2, s, a, b, h, h1, h2, i, j, k, o, o_, o1;
scanf("%d%d%d%d%d", &n, &m, &s, &a, &b), s--;
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
for (i = 0; i < n; i++) {
ej_[i] = (int *) malloc(eo[i] * sizeof *ej_[i]);
memcpy(ej_[i], ej[i], (eo_[i] = eo[i]) * sizeof *ej[i]);
}
memset(dd, 0x3f, n * sizeof *dd);
cnt1 = cnt2 = 0;
dd[s] = 0, qu1[cnt1++] = s;
h1 = 0, h2 = 0;
while (h1 < cnt1 || h2 < cnt2) {
i = h2 == cnt2 || h1 < cnt1 && dd[qu1[h1]] < dd[qu2[h2]] ? qu1[h1++] : qu2[h2++];
adj[i] = 1;
for (o = eo[i]; o--; ) {
j = ej[i][o];
adj[j] = 1;
if (dd[j] > dd[i] + a)
dd[j] = dd[i] + a, qu1[cnt1++] = j;
}
for (o = eo[i]; o--; ) {
j = ej[i][o];
o1 = 0;
for (o_ = 0; o_ < eo_[j]; o_++) {
k = ej_[j][o_];
if (adj[k])
ej_[j][o1++] = k;
else if (dd[k] > dd[i] + b)
dd[k] = dd[i] + b, qu2[cnt2++] = k;
}
eo_[j] = o1;
}
adj[i] = 0;
for (o = eo[i]; o--; ) {
j = ej[i][o];
adj[j] = 0;
}
}
for (i = 0; i < n; i++)
printf("%d\n", dd[i]);
return 0;
}
Compilation message (stderr)
cen.c: In function 'append':
cen.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
cen.c: In function 'main':
cen.c:39:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
39 | i = h2 == cnt2 || h1 < cnt1 && dd[qu1[h1]] < dd[qu2[h2]] ? qu1[h1++] : qu2[h2++];
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cen.c:23:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%d%d%d%d%d", &n, &m, &s, &a, &b), s--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cen.c:27:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d%d", &i, &j), 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |