Submission #485830

# Submission time Handle Problem Language Result Execution time Memory
485830 2021-11-09T13:32:02 Z rainboy Grad (COI14_grad) C++17
21 / 100
1500 ms 6340 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.cpp: In function 'void append(int, int)':
grad.cpp:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
grad.cpp: In function 'int main()':
grad.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)' 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.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |   scanf("%s", s);
      |   ~~~~~^~~~~~~~~
grad.cpp:100:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |    scanf("%d%d%d%d", &xx[n], &yy[n], &i, &j), i--, j--;
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.cpp:105:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |    scanf("%d%d", &i, &j), i--, j--;
      |    ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB 41 numbers
# Verdict Execution time Memory Grader output
1 Correct 11 ms 332 KB 300 numbers
# Verdict Execution time Memory Grader output
1 Correct 16 ms 376 KB 500 numbers
# Verdict Execution time Memory Grader output
1 Execution timed out 1586 ms 2944 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1587 ms 4176 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1577 ms 6340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1576 ms 4212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1584 ms 6164 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1583 ms 2196 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1583 ms 2888 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1584 ms 1968 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1585 ms 2008 KB Time limit exceeded
2 Halted 0 ms 0 KB -