This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* https://oj.uz/submission/101391 (Benq) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2000
#define M 2000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int ii[M], jj[M], aa[M], bb[M];
int *eh[N], eo[N];
void append(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;
}
long long ans[N]; int xx[N], yy[N], n, dx, dy; long long dd[N];
int pq[N], iq[1 + N], cnt;
int lt(int i, int j) {
return dd[i] < dd[j] || dd[i] == dd[j] && xx[i] < xx[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 solve() {
int i;
memset(dd, 0x3f, n * sizeof *dd);
dd[0] = 0, xx[0] = 0, yy[0] = 0, pq_add_last(0);
while (cnt) {
int i, o;
i = pq_remove_first();
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h], x = xx[i] + aa[h], y = yy[i] + bb[h];
long long d = dd[i] + (long long) aa[h] * dx + bb[h] * dy;
if (dd[j] > d || dd[j] == d && xx[j] > x) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, xx[j] = x, yy[j] = y, pq_up(j);
}
}
}
for (i = 0; i < n; i++)
if (dd[i] != INF)
ans[i] = min(ans[i], (long long) xx[i] * yy[i]);
}
int main() {
int m, h, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++) {
scanf("%d%d%d%d", &ii[h], &jj[h], &aa[h], &bb[h]), ii[h]--, jj[h]--;
append(ii[h], h), append(jj[h], h);
}
memset(ans, 0x3f, n * sizeof *ans);
dx = 0, dy = 1;
while (dy != 0) {
solve();
dx = 1, dy = 0;
for (i = 0; i < n; i++) {
int o;
if (dd[i] == INF)
continue;
for (o = eo[i]; o--; ) {
int x, y, x_, y_;
h = eh[i][o], j = i ^ ii[h] ^ jj[h];
x = xx[j], y = yy[j], x_ = xx[i] + aa[h], y_ = yy[i] + aa[h];
if (x <= x_)
continue;
if ((long long) dx * (y_ - y) - (long long) dy * (x - x_) < 0)
dx = x - x_, dy = y_ - y;
}
}
}
for (i = 1; i < n; i++)
printf("%lld\n", ans[i] == INF ? -1 : ans[i]);
return 0;
}
Compilation message (stderr)
ceste.c: In function 'append':
ceste.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
ceste.c: In function 'lt':
ceste.c:29:41: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
29 | return dd[i] < dd[j] || dd[i] == dd[j] && xx[i] < xx[j];
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
ceste.c: In function 'solve':
ceste.c:78:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
78 | if (dd[j] > d || dd[j] == d && xx[j] > x) {
| ~~~~~~~~~~~^~~~~~~~~~~~
ceste.c: In function 'main':
ceste.c:93:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
ceste.c:97:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d%d%d%d", &ii[h], &jj[h], &aa[h], &bb[h]), ii[h]--, jj[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |