# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
485828 |
2021-11-09T13:30:38 Z |
rainboy |
Grad (COI14_grad) |
C |
|
0 ms |
0 KB |
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define M (N * 2)
#define INF 1e13
int *eh[N], eo[N], xx[N], yy[N], n;
int ij[M], m; double ww[M];
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;
}
void add(int i, int j) {
int h = m++;
ij[h] = i ^ j, ww[h] = hypot(xx[j] - xx[i], yy[j] - yy[i]);
append(i, h), append(j, h);
}
int iq[1 + N], pq[N], cnt; double dd[N];
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 dijkstra(int s) {
int i;
for (i = 0; i < n; i++)
dd[i] = INF;
dd[s] = 0, pq_add_last(s);
while (cnt) {
int o;
i = pq_remove_first();
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
double d = dd[i] + ww[h];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
}
int main() {
int q;
scanf("%d%d%d%d%d", &xx[0], &yy[0], &xx[1], &yy[1], &q), n = 2;
eh[0] = (int *) malloc(2 * sizeof *eh[0]);
eh[1] = (int *) malloc(2 * sizeof *eh[1]);
add(0, 1);
while (q--) {
static char s[2];
int i, j;
scanf("%s", s);
if (s[0] == 'd') {
scanf("%d%d%d%d", &xx[n], &yy[n], &i, &j), i--, j--;
eh[n] = (int *) malloc(2 * sizeof *eh[n]);
add(n, i), add(n, j);
n++;
} else {
scanf("%d%d", &i, &j), i--, j--;
dijkstra(i);
printf("%f\n", dd[j]);
}
}
return 0;
}
Compilation message
grad.c: In function 'append':
grad.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
grad.c: In function 'main':
grad.c:90:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%d%d%d%d%d", &xx[0], &yy[0], &xx[1], &yy[1], &q), n = 2;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.c:98:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | scanf("%s", s);
| ^~~~~~~~~~~~~~
grad.c:100:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d%d%d%d", &xx[n], &yy[n], &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.c:105:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccsS3AVR.o: in function `add':
grad.c:(.text+0xe2): undefined reference to `hypot'
collect2: error: ld returned 1 exit status