답안 #471888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471888 2021-09-11T13:39:52 Z rainboy Ceste (COCI17_ceste) C
160 / 160
196 ms 484 KB
/* https://oj.uz/submission/101391 (Benq) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	2000
#define M	2000
#define INF	0x3f3f3f3f3f3f3f3fLL

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

int ii[M], jj[M], aa[M], bb[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 ans[N]; int xx[N], yy[N], n, dx, dy; long long dd[N];

int pq[N], iq[1 + N], cnt;

int lt(int i, int j) {
	return dd[i] < dd[j] || dd[i] == dd[j] && xx[i] < xx[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;
}

void solve() {
	int i;

	memset(dd, 0x3f, n * sizeof *dd);
	dd[0] = 0, xx[0] = 0, yy[0] = 0, pq_add_last(0);
	while (cnt) {
		int i, o;

		i = pq_remove_first();
		for (o = eo[i]; o--; ) {
			int h = eh[i][o], j = i ^ ii[h] ^ jj[h], x = xx[i] + aa[h], y = yy[i] + bb[h];
			long long d = dd[i] + (long long) aa[h] * dx + (long long) bb[h] * dy;

			if (dd[j] > d || dd[j] == d && xx[j] > x) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, xx[j] = x, yy[j] = y, pq_up(j); 
			}
		}
	}
	for (i = 0; i < n; i++)
		if (dd[i] != INF)
			ans[i] = min(ans[i], (long long) xx[i] * yy[i]);
}

int main() {
	int m, h, i, j;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d%d%d", &ii[h], &jj[h], &aa[h], &bb[h]), ii[h]--, jj[h]--;
		append(ii[h], h), append(jj[h], h);
	}
	memset(ans, 0x3f, n * sizeof *ans);
	dx = 0, dy = 1;
	while (dy != 0) {
		int dx_, dy_;

		dx_ = dx, dy_ = dy;
		solve();
		dx = 1, dy = 0;
		for (i = 0; i < n; i++) {
			int o;

			if (dd[i] == INF)
				continue;
			for (o = eo[i]; o--; ) {
				int x, y, x_, y_;

				h = eh[i][o], j = i ^ ii[h] ^ jj[h];
				x = xx[j], y = yy[j], x_ = xx[i] + aa[h], y_ = yy[i] + bb[h];
				if (x <= x_)
					continue;
				if ((long long) dx * (x - x_) - (long long) dy * (y_ - y) > 0)
					dx = y_ - y, dy = x - x_;
			}
		}
	}
	for (i = 1; i < n; i++)
		printf("%lld\n", ans[i] == INF ? -1 : ans[i]);
	return 0;
}

Compilation message

ceste.c: In function 'append':
ceste.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
ceste.c: In function 'lt':
ceste.c:29:41: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   29 |  return dd[i] < dd[j] || dd[i] == dd[j] && xx[i] < xx[j];
      |                          ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
ceste.c: In function 'solve':
ceste.c:78:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   78 |    if (dd[j] > d || dd[j] == d && xx[j] > x) {
      |                     ~~~~~~~~~~~^~~~~~~~~~~~
ceste.c: In function 'main':
ceste.c:103:12: warning: variable 'dy_' set but not used [-Wunused-but-set-variable]
  103 |   int dx_, dy_;
      |            ^~~
ceste.c:103:7: warning: variable 'dx_' set but not used [-Wunused-but-set-variable]
  103 |   int dx_, dy_;
      |       ^~~
ceste.c:93:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
ceste.c:97:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |   scanf("%d%d%d%d", &ii[h], &jj[h], &aa[h], &bb[h]), ii[h]--, jj[h]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 420 KB Output is correct
2 Correct 63 ms 332 KB Output is correct
3 Correct 12 ms 400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 332 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 13 ms 424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 448 KB Output is correct
2 Correct 66 ms 408 KB Output is correct
3 Correct 121 ms 424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 416 KB Output is correct
2 Correct 196 ms 452 KB Output is correct
3 Correct 7 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 412 KB Output is correct
2 Correct 112 ms 464 KB Output is correct
3 Correct 119 ms 472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 432 KB Output is correct
2 Correct 114 ms 472 KB Output is correct
3 Correct 111 ms 484 KB Output is correct