Submission #532336

#TimeUsernameProblemLanguageResultExecution timeMemory
532336rainboy날다람쥐 (JOI14_ho_t4)C11
100 / 100
203 ms17844 KiB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define INF	0x3f3f3f3f3f3f3f3fLL

long long max(long long a, long long b) { return a > b ? a : b; }

int *ej[N], *et[N], eo[N];

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

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

long long dd[N]; int iq[1 + N], 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 hh[N];
	int n, m, x, h, i, j, t;

	scanf("%d%d%d", &n, &m, &x);
	for (i = 0; i < n; i++)
		scanf("%d", &hh[i]);
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		et[i] = (int *) malloc(2 * sizeof *et[i]);
	}
	for (h = 0; h < m; h++) {
		scanf("%d%d%d", &i, &j, &t), i--, j--;
		if (hh[i] >= t)
			append(i, j, t);
		if (hh[j] >= t)
			append(j, i, t);
	}
	memset(dd, 0x3f, n * sizeof *dd);
	dd[0] = 0, pq_add_last(0);
	while (cnt) {
		int o;

		i = pq_remove_first();
		if (i == n - 1) {
			printf("%lld\n", dd[i] * 2 + (hh[i] - x));
			return 0;
		}
		for (o = eo[i]; o--; ) {
			long long d;

			j = ej[i][o], d = max(dd[i] + et[i][o], x - hh[j]);
			if (dd[j] > d) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, pq_up(j);
			}
		}
	}
	printf("-1\n");
	return 0;
}

Compilation message (stderr)

2014_ho_t4.c: In function 'append':
2014_ho_t4.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
2014_ho_t4.c: In function 'main':
2014_ho_t4.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |  scanf("%d%d%d", &n, &m, &x);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t4.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   scanf("%d", &hh[i]);
      |   ^~~~~~~~~~~~~~~~~~~
2014_ho_t4.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   scanf("%d%d%d", &i, &j, &t), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...