#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define M (N * 2)
#define INF 1e13
double hypot_(int x, int y) {
return sqrt((long long) x * x + (long long) y * y);
}
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:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
grad.c: In function 'main':
grad.c:94:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d%d%d%d%d", &xx[0], &yy[0], &xx[1], &yy[1], &q), n = 2;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.c:102:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%s", s);
| ^~~~~~~~~~~~~~
grad.c:104:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf("%d%d%d%d", &xx[n], &yy[n], &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.c:109:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccv2zhdq.o: in function `add':
grad.c:(.text+0x161): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccv2zhdq.o: in function `hypot_':
grad.c:(.text+0x2e): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status