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 INF 0x3f3f3f3f
int *ej[N], *ew[N], eo[N];
void append(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;
}
int dd[N], 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;
}
int ds[N], ww[N];
int find(int i) {
return ds[i] < 0 ? i : find(ds[i]);
}
void join(int i, int j, int w) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j, ww[i] = w;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, ww[j] = w;
}
}
int query(int i, int j) {
int w = INF;
while (i != j)
if (ww[i] > ww[j])
w = ww[i], i = ds[i];
else
w = ww[j], j = ds[j];
return w;
}
int main() {
static int ii[N];
static char active[N];
int n, m, k, q, h, i, j, w, o;
scanf("%d%d", &n, &m);
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(i, j, w), append(j, i, w);
}
scanf("%d", &k);
memset(dd, 0x3f, n * sizeof *dd);
while (k--) {
scanf("%d", &i), i--;
dd[i] = 0, pq_add_last(i);
}
for (h = 0; h < n; h++) {
ii[h] = i = pq_remove_first();
for (o = eo[i]; o--; ) {
int d;
j = ej[i][o], w = ew[i][o], d = dd[i] + w;
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
memset(ds, -1, n * sizeof *ds), memset(ww, -1, n * sizeof *ww);
for (h = n - 1; h >= 0; h--) {
i = ii[h];
active[i] = 1;
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (active[j])
join(i, j, dd[i]);
}
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &i, &j), i--, j--;
printf("%d\n", query(i, j));
}
return 0;
}
Compilation message (stderr)
plan.c: In function 'append':
plan.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
plan.c: In function 'main':
plan.c:93:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
plan.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d%d%d", &i, &j, &w), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.c:102:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%d", &k);
| ^~~~~~~~~~~~~~~
plan.c:105:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
plan.c:131:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
plan.c:133:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | 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... |