Submission #924803

# Submission time Handle Problem Language Result Execution time Memory
924803 2024-02-09T17:18:27 Z rainboy None (KOI16_gas) C
100 / 100
440 ms 788 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	2500
#define M	4000
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }

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;
}

int cc[N];

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

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

long long dd[N], 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;
}

int main() {
	static int ii[N];
	int n, m, h, i, j, o, c, w;
	long long d;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &cc[i]);
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		ew[i] = (int *) malloc(2 * sizeof *ew[i]);
	}
	for (h = 0; h < m; h++) {
		scanf("%d%d%d", &i, &j, &w), i--, j--;
		append(i, j, w), append(j, i, w);
	}
	for (i = 0; i < n; i++)
		ii[i] = i;
	sort(ii, 0, n);
	memset(dd, 0x3f, n * sizeof *dd);
	dd[0] = 0;
	for (h = n - 1; h >= 0; h--) {
		i = ii[h], c = cc[i];
		if (dd[i] == INF)
			continue;
		memset(dd_, 0x3f, n * sizeof *dd_);
		dd_[i] = dd[i], pq_add_last(i);
		while (cnt) {
			i = pq_remove_first();
			dd[i] = min(dd[i], dd_[i]);
			for (o = eo[i]; o--; ) {
				j = ej[i][o], w = ew[i][o], d = dd_[i] + w * c;
				if (dd_[j] > d) {
					if (dd_[j] == INF)
						pq_add_last(j);
					dd_[j] = d, pq_up(j);
				}
			}
		}
	}
	printf("%lld\n", dd[n - 1]);
	return 0;
}

Compilation message

gas.c: In function 'append':
gas.c:22:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   22 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
gas.c: In function 'main':
gas.c:93:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
gas.c:95:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |   scanf("%d", &cc[i]);
      |   ^~~~~~~~~~~~~~~~~~~
gas.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d%d%d", &i, &j, &w), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 424 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 428 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 100 ms 604 KB Output is correct
3 Correct 99 ms 708 KB Output is correct
4 Correct 95 ms 604 KB Output is correct
5 Correct 37 ms 604 KB Output is correct
6 Correct 14 ms 600 KB Output is correct
7 Correct 60 ms 604 KB Output is correct
8 Correct 64 ms 600 KB Output is correct
9 Correct 42 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 2 ms 700 KB Output is correct
3 Correct 2 ms 684 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 704 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 424 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 428 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 5 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 3 ms 348 KB Output is correct
17 Correct 19 ms 348 KB Output is correct
18 Correct 28 ms 348 KB Output is correct
19 Correct 15 ms 344 KB Output is correct
20 Correct 15 ms 348 KB Output is correct
21 Correct 14 ms 528 KB Output is correct
22 Correct 15 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 424 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 428 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 100 ms 604 KB Output is correct
16 Correct 99 ms 708 KB Output is correct
17 Correct 95 ms 604 KB Output is correct
18 Correct 37 ms 604 KB Output is correct
19 Correct 14 ms 600 KB Output is correct
20 Correct 60 ms 604 KB Output is correct
21 Correct 64 ms 600 KB Output is correct
22 Correct 42 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 2 ms 700 KB Output is correct
26 Correct 2 ms 684 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 2 ms 704 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 5 ms 344 KB Output is correct
36 Correct 2 ms 344 KB Output is correct
37 Correct 3 ms 348 KB Output is correct
38 Correct 19 ms 348 KB Output is correct
39 Correct 28 ms 348 KB Output is correct
40 Correct 15 ms 344 KB Output is correct
41 Correct 15 ms 348 KB Output is correct
42 Correct 14 ms 528 KB Output is correct
43 Correct 15 ms 344 KB Output is correct
44 Correct 107 ms 708 KB Output is correct
45 Correct 87 ms 700 KB Output is correct
46 Correct 62 ms 600 KB Output is correct
47 Correct 239 ms 756 KB Output is correct
48 Correct 137 ms 760 KB Output is correct
49 Correct 296 ms 788 KB Output is correct
50 Correct 237 ms 776 KB Output is correct
51 Correct 175 ms 772 KB Output is correct
52 Correct 440 ms 764 KB Output is correct