Submission #924803

#TimeUsernameProblemLanguageResultExecution timeMemory
924803rainboy주유소 (KOI16_gas)C11
100 / 100
440 ms788 KiB
#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 (stderr)

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 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...