답안 #485831

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485831 2021-11-09T13:34:48 Z rainboy Grad (COI14_grad) C
컴파일 오류
0 ms 0 KB
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define N	100100
#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() {
	static int ii[N], jj[N];
	int q, h;

	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);
	for (h = 0; h < q; h++) {
		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++;
			ii[h] = jj[h] = -1;
		} else {
			scanf("%d%d", &i, &j), i--, j--;
			if (q <= 1000) {
				dijkstra(i);
				printf("%f\n", dd[j]);
			} else
				ii[h] = i, jj[h] = j;
		}
	}
	if (q > 1000) {
		for (h = 0; h < q; h++)
			if (ii[h] != -1) {
				dijkstra(ii[h]);
				break;
			}
		for (h = 0; h < q; h++)
			if (jj[h] != -1)
				printf("%f\n", dd[jj[h]]);
	}
	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:91:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |  scanf("%d%d%d%d%d", &xx[0], &yy[0], &xx[1], &yy[1], &q), n = 2;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |   scanf("%s", s);
      |   ^~~~~~~~~~~~~~
grad.c:101:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |    scanf("%d%d%d%d", &xx[n], &yy[n], &i, &j), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.c:107:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |    scanf("%d%d", &i, &j), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc6mJYs9.o: in function `add':
grad.c:(.text+0xe2): undefined reference to `hypot'
collect2: error: ld returned 1 exit status