Submission #955831

# Submission time Handle Problem Language Result Execution time Memory
955831 2024-03-31T14:04:02 Z rainboy Farm and factory (CERC12_F) C
1 / 1
866 ms 47924 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define INF	0x3f3f3f3f3f3f3f3fLL

long long abs_(long long a) { return a > 0 ? a : -a; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int *ej[N], *ew[N], eo[N];

void append(int i, int j, int w) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0) {
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
		ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
	}
	ej[i][o] = j, ew[i][o] = w;
}

long long *dd; int iq[N + 1], pq[N], cnt;

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 n, int s) {
	int i, o;

	memset(dd, 0x3f, n * sizeof *dd);
	dd[s] = 0, pq_add_last(s);
	while (cnt) {
		i = pq_remove_first();
		for (o = eo[i]; o--; ) {
			int j = ej[i][o], w = ew[i][o];
			long long d = dd[i] + w;

			if (dd[j] > d) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, pq_up(j);
			}
		}
	}
}

void sort(long long *aa, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r;
		long long a = aa[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (aa[j] == a)
				j++;
			else if (aa[j] < a) {
				tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
			}
		sort(aa, l, i);
		l = k;
	}
}

int main() {
	int t;

	scanf("%d", &t);
	while (t--) {
		static long long dd0[N], dd1[N], xx[N], yy[N];
		int n, m, h, i, j, w;
		long long x, y, sum;

		scanf("%d%d", &n, &m);
		for (i = 0; i < n; i++) {
			ej[i] = (int *) malloc(2 * sizeof *ej[i]);
			ew[i] = (int *) malloc(2 * sizeof *ew[i]);
			eo[i] = 0;
		}
		for (h = 0; h < m; h++) {
			scanf("%d%d%d", &i, &j, &w), i--, j--;
			append(i, j, w), append(j, i, w);
		}
		dd = dd0, dijkstra(n, 0);
		dd = dd1, dijkstra(n, 1);
		for (i = 0; i < n; i++) {
			xx[i] = dd0[i] + dd1[i];
			yy[i] = dd0[i] - dd1[i];
		}
		sum = 0;
		sort(xx, 0, n);
		x = xx[n / 2];
		for (i = 0; i < n; i++)
			sum += abs_(xx[i] - x);
		sort(yy, 0, n);
		y = yy[n / 2];
		for (i = 0; i < n; i++)
			sum += abs_(yy[i] - y);
		printf("%.12f\n", (double) sum / 2 / n);
		for (i = 0; i < n; i++)
			free(ej[i]), free(ew[i]);
	}
	return 0;
}

Compilation message

F.c: In function 'append':
F.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
F.c: In function 'main':
F.c:108:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
F.c:114:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |   scanf("%d%d", &n, &m);
      |   ^~~~~~~~~~~~~~~~~~~~~
F.c:121:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |    scanf("%d%d%d", &i, &j, &w), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 25 ms 5136 KB Output is correct
3 Correct 866 ms 47924 KB Output is correct