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 1e9
#define LINF 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 *) malloc(2 * sizeof *eh[i]);
eh[i][o] = h;
}
long long ans[N]; int xx[N], yy[N], n; double dd[N], z;
int pq[N], iq[1 + 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 solve() {
int i;
for (i = 1; i < n; i++)
dd[i] = INF;
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];
double d = dd[i] + aa[h] * z + bb[h] * (1 - z);
if (dd[j] > d) {
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);
z = 0;
while (1) {
double z_;
solve();
if (z == 1)
break;
z_ = 1;
for (i = 0; i < n; i++) {
int o;
if (dd[i] == INF)
continue;
for (o = eo[i]; o--; ) {
int x, y;
double z1;
h = eh[i][o], j = i ^ ii[h] ^ jj[h], x = xx[i] + aa[h] - xx[j], y = yy[i] + bb[h] - yy[j];
if (x == y)
continue;
z1 = -y / (x - y);
if (z1 > z && z_ > z1)
z_ = z1;
}
}
z = z_;
}
for (i = 1; i < n; i++)
printf("%lld\n", ans[i] == LINF ? -1 : ans[i]);
return 0;
}
Compilation message (stderr)
ceste.c: In function 'append':
ceste.c:20:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
20 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
ceste.c: In function 'main':
ceste.c:95:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
ceste.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | 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... |