Submission #787952

#TimeUsernameProblemLanguageResultExecution timeMemory
787952rainboyVoting Cities (NOI22_votingcity)C11
100 / 100
94 ms23620 KiB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	5000
#define N_	(N << 5)
#define INF	0x3f3f3f3f3f3f3f3fLL

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

int *ej[N_], *ew[N_], eo[N_], 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[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 ww[5];
	int n, m, k, q, g, i, j, b, o, w;
	long long d, d_;

	scanf("%d%d%d", &n, &m, &k), n_ = n << 5;
	for (i = 0; i < n_; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		ew[i] = (int *) malloc(2 * sizeof *ew[i]);
	}
	memset(dd, 0x3f, n_ * sizeof *dd);
	while (k--) {
		scanf("%d", &i);
		dd[i << 5 | 0] = 0, pq_add_last(i << 5 | 0);
	}
	while (m--) {
		scanf("%d%d%d", &i, &j, &w);
		for (b = 0; b < 1 << 5; b++) {
			append(j << 5 | b, i << 5 | b, w);
			for (g = 0; g < 5; g++)
				if ((b & 1 << g) == 0)
					append(j << 5 | b, i << 5 | b | 1 << g, w / 10 * (9 - g));
		}
	}
	while (cnt) {
		i = pq_remove_first();
		for (o = eo[i]; o--; ) {
			j = ej[i][o], w = ew[i][o], d = dd[i] + w;
			if (dd[j] > d) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, pq_up(j);
			}
		}
	}
	scanf("%d", &q);
	while (q--) {
		scanf("%d", &i);
		for (g = 0; g < 5; g++)
			scanf("%d", &ww[g]);
		d_ = INF;
		for (b = 0; b < 1 << 5; b++) {
			d = dd[i << 5 | b];
			for (g = 0; g < 5; g++)
				if ((b & 1 << g) != 0)
					d = ww[g] == -1 ? INF : min(d + ww[g], INF);
			d_ = min(d_, d);
		}
		if (d_ == INF)
			d_ = -1;
		printf("%lld\n", d_);
	}
	return 0;
}

Compilation message (stderr)

Main.c: In function 'append':
Main.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   16 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Main.c: In function 'main':
Main.c:65:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |  scanf("%d%d%d", &n, &m, &k), n_ = n << 5;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:72:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |   scanf("%d", &i);
      |   ^~~~~~~~~~~~~~~
Main.c:76:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |   scanf("%d%d%d", &i, &j, &w);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:95:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
Main.c:97:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |   scanf("%d", &i);
      |   ^~~~~~~~~~~~~~~
Main.c:99:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |    scanf("%d", &ww[g]);
      |    ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...