Submission #485832

# Submission time Handle Problem Language Result Execution time Memory
485832 2021-11-09T13:35:00 Z rainboy Grad (COI14_grad) C++17
45 / 100
118 ms 8408 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.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:91:7: warning: ignoring return value of 'int scanf(const char*, ...)' 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.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |   scanf("%s", s);
      |   ~~~~~^~~~~~~~~
grad.cpp:101:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |    scanf("%d%d%d%d", &xx[n], &yy[n], &i, &j), i--, j--;
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grad.cpp:107:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |    scanf("%d%d", &i, &j), i--, j--;
      |    ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB 41 numbers
# Verdict Execution time Memory Grader output
1 Correct 12 ms 348 KB 300 numbers
# Verdict Execution time Memory Grader output
1 Correct 16 ms 332 KB 500 numbers
# Verdict Execution time Memory Grader output
1 Correct 36 ms 4424 KB 15000 numbers
2 Correct 39 ms 5180 KB 15000 numbers
3 Correct 42 ms 5000 KB 30000 numbers
# Verdict Execution time Memory Grader output
1 Correct 57 ms 6788 KB 28333 numbers
2 Correct 82 ms 7704 KB 28333 numbers
3 Correct 57 ms 6424 KB 40000 numbers
# Verdict Execution time Memory Grader output
1 Correct 70 ms 6912 KB 50000 numbers
2 Correct 118 ms 8280 KB 50000 numbers
3 Correct 76 ms 8224 KB 50000 numbers
# Verdict Execution time Memory Grader output
1 Correct 84 ms 7056 KB 55000 numbers
2 Correct 79 ms 8076 KB 55000 numbers
3 Correct 78 ms 8244 KB 50000 numbers
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 6944 KB 2nd numbers differ - expected: '1203360.41628', found: '2061890.86421', error = '858530.44793'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 8292 KB 2nd numbers differ - expected: '3503.52637', found: '3024.05638', error = '479.46998'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 81 ms 7544 KB 2nd numbers differ - expected: '36724196.98716', found: '121235989.48981', error = '84511792.50265'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 8408 KB 3rd numbers differ - expected: '934447.35820', found: '733629.34687', error = '200818.01133'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 7584 KB 2nd numbers differ - expected: '465.09769', found: '471.09680', error = '5.99911'
2 Halted 0 ms 0 KB -