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>
#define N 200000
#define M 200000
#define INF 0x3f3f3f3f
int max(int a, int b) { return a > b ? a : b; }
int *eh[N], eo[N], *fh[N], fo[N], dd[N], n;
int ii[M], jj[M], ww[M], xx[M], yy[M], m;
void append(int **eh, int *eo, int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int pq[N + M], *iq[N + 1], cnt[N + 1], mode;
int (*compare)(int, int);
int lt(int i, int j) {
return mode < n ? xx[i - n] < xx[j - n] : dd[i] > dd[j];
}
int p2(int p) {
return (p *= 2) > cnt[mode] ? 0 : (p < cnt[mode] && lt(iq[mode][p + 1], iq[mode][p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[mode][q]); p = q)
iq[mode][pq[j] = p] = j;
iq[mode][pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[mode][q], i); p = q)
iq[mode][pq[j] = p] = j;
iq[mode][pq[i] = p] = i;
}
void pq_add(int i) {
pq[i] = ++cnt[mode], pq_up(i);
}
int pq_first() {
return iq[mode][1];
}
int pq_remove_first() {
int i = iq[mode][1], j = iq[mode][cnt[mode]--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int main() {
int h, i, j, x, y, o;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
fh[i] = (int *) malloc(2 * sizeof *fh[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d%d%d", &i, &j, &x, &y), i--, j--;
ii[h] = i, jj[h] = j, xx[h] = x, yy[h] = y;
append(eh, eo, i, h), append(fh, fo, j, h);
}
for (i = 0; i < n; i++) {
mode = i;
iq[mode] = (int *) malloc((eo[i] + 1) * sizeof *iq[mode]);
for (o = eo[i]; o--; ) {
h = eh[i][o];
pq_add(n + h);
}
dd[i] = cnt[i] == 0 ? INF : xx[pq_first() - n];
}
mode = n;
iq[mode] = (int *) malloc((n + 1) * sizeof *iq[mode]);
for (i = 0; i < n; i++)
pq_add(i);
while (cnt[mode = n]) {
j = pq_remove_first();
for (o = fo[j]; o--; ) {
h = fh[j][o], i = ii[h];
xx[h] = max(xx[h], dd[j] == INF ? INF : dd[j] - yy[h]);
mode = i, pq_dn(n + h);
dd[i] = cnt[i] == 0 ? INF : xx[pq_first() - n];
mode = n, pq_up(i);
}
}
for (i = 0; i < n; i++)
printf("%d ", dd[i] == INF ? -1 : dd[i]);
printf("\n");
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
16 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:75:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d%d%d%d", &i, &j, &x, &y), 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... |