Submission #703240

# Submission time Handle Problem Language Result Execution time Memory
703240 2023-02-26T16:59:34 Z rainboy Ferries (NOI13_ferries) C
40 / 40
201 ms 16540 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define INF	0x3f3f3f3f3f3f3f3fLL

unsigned int X = 12345;

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

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

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

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

long long dd[N]; 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 sort(int *aa, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, 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() {
	static char visited[N];
	int n, m, i, j, w, o;

	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]);
	}
	while (m--) {
		scanf("%d%d%d", &i, &j, &w), i--, j--;
		append(ej, eo, j, i), append(ew, eo_, i, w);
	}
	for (i = 0; i < n; i++)
		sort(ew[i], 0, eo_[i]);
	memset(dd, 0x3f, n * sizeof *dd), dd[n - 1] = 0, pq_add_last(n - 1);
	while (cnt) {
		i = pq_remove_first();
		if (i == 0) {
			printf("%lld\n", dd[i]);
			return 0;
		}
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (!visited[j]) {
				long long d = dd[i] + ew[j][--eo_[j]];

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

Compilation message

ferries.c: In function 'append':
ferries.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
ferries.c: In function 'main':
ferries.c:84:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
ferries.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   scanf("%d%d%d", &i, &j, &w), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 440 KB Output is correct
3 Correct 9 ms 1708 KB Output is correct
4 Correct 120 ms 14372 KB Output is correct
5 Correct 116 ms 14296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
3 Correct 8 ms 1648 KB Output is correct
4 Correct 52 ms 7312 KB Output is correct
5 Correct 58 ms 11956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 1796 KB Output is correct
2 Correct 12 ms 1832 KB Output is correct
3 Correct 161 ms 16420 KB Output is correct
4 Correct 174 ms 16460 KB Output is correct
5 Correct 182 ms 15744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 16392 KB Output is correct
2 Correct 169 ms 16396 KB Output is correct
3 Correct 148 ms 16420 KB Output is correct
4 Correct 187 ms 16476 KB Output is correct
5 Correct 201 ms 16540 KB Output is correct