結果

問題 No.1814 Uribo Road (Easy)
ユーザー 👑 ygussanyygussany
提出日時 2022-01-15 20:01:26
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,110 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 31,872 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 18:11:57
合計ジャッジ時間 1,510 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 10 ms
5,376 KB
testcase_25 AC 10 ms
5,376 KB
testcase_26 AC 18 ms
5,376 KB
testcase_27 AC 22 ms
5,376 KB
testcase_28 AC 20 ms
5,376 KB
testcase_29 AC 25 ms
5,376 KB
testcase_30 AC 20 ms
5,376 KB
testcase_31 AC 20 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 6 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 7 ms
5,376 KB
testcase_42 AC 4 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 7 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 8 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int bit[13] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};

typedef struct {
	int key, id[2];
} data;

typedef struct {
	data obj[40000000];
	int size;
} min_heap;

void push(min_heap* h, data x)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key < h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j <<= 1;
		} else break;
	}
	return output;
}

typedef struct Edge {
	struct Edge *next;
	int v, cost, flag;
} edge;

int main()
{
	int i, j, N, M, K, u, w, c, R[12];
	edge *adj[10001] = {}, e[40001], *p;
	scanf("%d %d %d", &N, &M, &K);
	for (i = 0; i < K; i++) scanf("%d", &(R[i]));
	for (i = 0; i < M; i++) {
		scanf("%d %d %d", &u, &w, &c);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].cost = c;
		e[i*2+1].cost = c;
		e[i*2].flag = 0;
		e[i*2+1].flag = 0;
		for (j = 0; j < K; j++) {
			if (R[j] == i + 1) {
				e[i*2].flag = bit[j];
				e[i*2+1].flag = bit[j];
			}
		}
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	const int sup = ((1 << 30) - 1) << 1;
	int dist[10001][4096];
	data d;
	min_heap h;
	h.size = 0;
	for (u = 1; u <= N; u++) for (i = 0; i < bit[K]; i++) dist[u][i] = sup;
	dist[1][0] = 0;
	d.key = 0;
	d.id[0] = 1;
	d.id[1] = 0;
	push(&h, d);
	while (h.size > 0) {
		d = pop(&h);
		u = d.id[0];
		i = d.id[1];
		if (d.key != dist[u][i]) continue;
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			j = i | p->flag;
			if (dist[w][j] > dist[u][i] + p->cost) {
				dist[w][j] = dist[u][i] + p->cost;
				d.key = dist[w][j];
				d.id[0] = w;
				d.id[1] = j;
				push(&h, d);
			}
		}
	}
	printf("%d\n", dist[N][bit[K]-1]);
	fflush(stdout);
	return 0;
}
0