結果

問題 No.134 走れ!サブロー君
ユーザー bal4ubal4u
提出日時 2019-08-07 21:44:22
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 2,638 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 31,072 KB
実行使用メモリ 5,952 KB
最終ジャッジ日時 2023-09-25 23:16:28
合計ジャッジ時間 1,587 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 16 ms
4,380 KB
testcase_08 AC 47 ms
5,952 KB
testcase_09 AC 51 ms
5,856 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 0 ms
4,380 KB
testcase_12 AC 0 ms
4,376 KB
testcase_13 AC 0 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:8:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:14:24: 備考: in expansion of macro ‘gc’
   14 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder 134 走れ!サブロー君
// 2019.8.7 bal4u

#include <stdio.h>

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

double getdbl() {   // 非負実数の入力
	double x, y;
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	if (c == '.') {
		x = 0;
		y = 1, c = gc();
		do y *= 0.1, x += y * (c & 0xf), c = gc(); while (c >= '0');
		x += n;
	} else x = n;
	return x;
}

//// 優先度付きキュー(ダイクストラ法用)
#define QMAX 2000000
typedef struct { int n, b; double w, t; } QUE;
QUE que[QMAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i) {
	int l, r, min;
	QUE qt;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		qt = que[i]; que[i] = que[min]; que[min] = qt;
		min_heapify(min);
	}
}

void deq() {
	que[0] = que[--qsize];
	min_heapify(0);
}

void enq(int n, double t, int b, double w) {
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].t = t, que[i].n = n, que[i].b = b, que[i].w = w;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i]; que[i] = que[min]; que[min] = qt;
		i = min;
	}
}

//// 本問題関連
#define ABS(x)  ((x)>=0?(x):-(x))

#define MAX 16
int N;
int X[MAX], Y[MAX]; double W[MAX];
int d[MAX][MAX];
char vis[MAX][1<<14];

double dijkstra() {
	int i, n, b, lim;
	double w, t, ans;

	lim = (1<<(N+1))-1, ans = 1e20;
	w = 0; for (i = 0; i <= N; i++) w += W[i];
	enq(0, 0.0, 1, w);
	while (qsize) {
		n = que[0].n, t = que[0].t, b = que[0].b, w = que[0].w, deq();
		if (W[n] > 0.0) t += W[n], w -= W[n];
//printf("n=%d, t=%lf, b=%x, w=%lf, ans=%lf\n", n, t, b, w, ans);
		if (b == lim) {
			t += d[n][0]*5.0/6;
			if (t < ans) ans = t;
			continue;
		}
		if (vis[n][b]) continue;
		vis[n][b] = 1;
		for (i = 1; i <= N; i++) {
			if ((b >> i) & 1) continue;
			if (!vis[i][b|(1<<i)]) enq(i, t+d[n][i]*(w+100)/120, b|(1<<i), w);
		}
	}
	return ans;
}

int main()
{
	int i, j, n;

	X[0] = in(), Y[0] = in(), N = in();
	n = 0; while (N--) {
		int x = in(), y = in(); double w = getdbl();
		for (i = 0; i <= n; i++) {
			if (X[i] == x && Y[i] == y) { W[i] += w; break; }
		}
		if (i > n) n++, X[n] = x, Y[n] = y, W[n] = w;
	}
	N = n;
	for (i = 0; i < N; i++) for (j = i+1; j <= N; j++)
		d[i][j] = d[j][i] = ABS(X[j]-X[i]) + ABS(Y[j]-Y[i]);
	
	printf("%.12lf\n", dijkstra());
	return 0;
}
0