답안 #544728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544728 2022-04-02T16:12:57 Z rainboy Cities (BOI16_cities) C
100 / 100
1904 ms 37920 KB
#pragma GCC optimize("O3")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define M	200000
#define K	5
#define INF	0x3f3f3f3f3f3f3f3fLL

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

int ij[M], ww[M];
int *eh[N], eo[N];

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

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

int lt(int i, int j) { return dp_[i] < dp_[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 gg[N];
	static long long dp[1 << K][N];
	int n, k, m, g, h, i, j, b;
	long long ans;

	scanf("%d%d%d", &n, &k, &m);
	memset(gg, -1, n * sizeof *gg);
	for (g = 0; g < k; g++) {
		scanf("%d", &i), i--;
		gg[i] = g;
	}
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
		ij[h] = i ^ j;
		append(i, h), append(j, h);
	}
	for (b = 0; b < 1 << k; b++)
		memset(dp[b], 0x3f, n * sizeof *dp[b]);
	for (i = 0; i < n; i++)
		dp[gg[i] == -1 ? 0 : 1 << gg[i]][i] = 0;
	for (b = 0; b < 1 << k; b++) {
		int c, b_;

		dp_ = dp[b];
		c = b, b_ = 0;
		do
			for (i = 0; i < n; i++)
				dp_[i] = min(dp_[i], dp[b_][i] + dp[b ^ b_][i]);
		while (b_ = b_ - c & c);
		cnt = 0;
		for (i = 0; i < n; i++)
			pq_add_last(i);
		for (h = cnt / 2; h > 0; h--)
			pq_dn(iq[h]);
		while (cnt) {
			int o;
			long long x;

			i = pq_remove_first();
			for (o = 0; o < eo[i]; o++) {
				h = eh[i][o], j = i ^ ij[h], x = dp_[i] + ww[h];
				if (dp_[j] > x)
					dp_[j] = x, pq_up(j);
			}
		}
	}
	b = (1 << k) - 1;
	ans = INF;
	for (i = 0; i < n; i++)
		ans = min(ans, dp[b][i]);
	printf("%lld\n", ans);
	return 0;
}

Compilation message

cities.c: In function 'append':
cities.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
cities.c: In function 'main':
cities.c:93:18: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   93 |   while (b_ = b_ - c & c);
      |               ~~~^~~
cities.c:93:10: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   93 |   while (b_ = b_ - c & c);
      |          ^~
cities.c:68:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |  scanf("%d%d%d", &n, &k, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
cities.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |   scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 466 ms 19148 KB Output is correct
2 Correct 502 ms 18976 KB Output is correct
3 Correct 302 ms 14924 KB Output is correct
4 Correct 74 ms 7796 KB Output is correct
5 Correct 273 ms 15948 KB Output is correct
6 Correct 65 ms 7608 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 596 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 984 ms 25412 KB Output is correct
2 Correct 924 ms 25188 KB Output is correct
3 Correct 718 ms 21200 KB Output is correct
4 Correct 464 ms 17040 KB Output is correct
5 Correct 156 ms 9804 KB Output is correct
6 Correct 80 ms 8760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1865 ms 37916 KB Output is correct
2 Correct 1904 ms 37920 KB Output is correct
3 Correct 1807 ms 37752 KB Output is correct
4 Correct 1491 ms 33816 KB Output is correct
5 Correct 926 ms 23480 KB Output is correct
6 Correct 219 ms 11136 KB Output is correct
7 Correct 109 ms 8920 KB Output is correct