結果

問題 No.1480 Many Complete Graphs
ユーザー 👑 ygussanyygussany
提出日時 2021-04-16 23:25:00
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 2,537 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 30,396 KB
実行使用メモリ 24,440 KB
最終ジャッジ日時 2023-09-16 03:01:35
合計ジャッジ時間 4,196 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,176 KB
testcase_01 AC 2 ms
10,056 KB
testcase_02 AC 2 ms
10,244 KB
testcase_03 AC 3 ms
9,912 KB
testcase_04 AC 3 ms
10,420 KB
testcase_05 AC 3 ms
10,228 KB
testcase_06 AC 3 ms
10,028 KB
testcase_07 AC 3 ms
10,356 KB
testcase_08 AC 3 ms
10,240 KB
testcase_09 AC 3 ms
10,152 KB
testcase_10 AC 4 ms
11,332 KB
testcase_11 AC 2 ms
10,308 KB
testcase_12 AC 3 ms
10,804 KB
testcase_13 AC 9 ms
13,324 KB
testcase_14 AC 8 ms
12,560 KB
testcase_15 AC 18 ms
15,396 KB
testcase_16 AC 11 ms
12,756 KB
testcase_17 AC 12 ms
13,300 KB
testcase_18 AC 4 ms
10,800 KB
testcase_19 AC 12 ms
13,540 KB
testcase_20 AC 16 ms
15,276 KB
testcase_21 AC 10 ms
14,364 KB
testcase_22 AC 7 ms
12,912 KB
testcase_23 AC 19 ms
19,244 KB
testcase_24 AC 32 ms
19,356 KB
testcase_25 AC 38 ms
19,372 KB
testcase_26 AC 40 ms
19,704 KB
testcase_27 AC 33 ms
19,632 KB
testcase_28 AC 34 ms
22,772 KB
testcase_29 AC 35 ms
19,372 KB
testcase_30 AC 35 ms
21,008 KB
testcase_31 AC 35 ms
19,892 KB
testcase_32 AC 35 ms
24,440 KB
testcase_33 AC 35 ms
19,308 KB
testcase_34 AC 35 ms
19,548 KB
testcase_35 AC 35 ms
19,776 KB
testcase_36 AC 35 ms
22,000 KB
testcase_37 AC 35 ms
19,420 KB
testcase_38 AC 36 ms
19,548 KB
testcase_39 AC 37 ms
21,260 KB
testcase_40 AC 34 ms
19,352 KB
testcase_41 AC 34 ms
19,900 KB
testcase_42 AC 36 ms
19,576 KB
testcase_43 AC 35 ms
22,892 KB
testcase_44 AC 36 ms
19,600 KB
testcase_45 AC 35 ms
19,488 KB
testcase_46 AC 34 ms
19,364 KB
testcase_47 AC 70 ms
23,084 KB
testcase_48 AC 70 ms
21,792 KB
testcase_49 AC 75 ms
21,964 KB
testcase_50 AC 24 ms
22,280 KB
testcase_51 AC 70 ms
21,376 KB
testcase_52 AC 49 ms
19,732 KB
testcase_53 AC 51 ms
19,560 KB
testcase_54 AC 47 ms
23,216 KB
testcase_55 AC 51 ms
21,988 KB
testcase_56 AC 49 ms
22,920 KB
testcase_57 AC 29 ms
21,536 KB
testcase_58 AC 39 ms
19,572 KB
evil_aftercontest.txt AC 79 ms
32,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v;
	long long cost;
} list;

typedef struct {
	long long key;
	int id;
} data;
 
typedef struct {
	data obj[800001];
	int size;
} min_heap;
 
void push(data x, min_heap* h)
{
	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 = i << 1;
		} else break;
	}
	return output;
}

int main()
{
	int i, j, k, m, n, u, w, N, M;
	long long c;
	list *adj[500001] = {}, e[800001], *p;
	scanf("%d %d", &N, &M);
	for (i = 1, m = 0, n = N; i <= M; i++, n += 4) {
		scanf("%d %lld", &k, &c);
		for (j = 1; j <= k; j++) {
			scanf("%d", &u);
			if (u % 2 == 0) {
				w = n + 1;
				e[m].v = w;
				e[m].cost = c + u / 2;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				w = n + 2;
				e[m].v = w;
				e[m].cost = c + u / 2;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				w = n + 1;
				e[m].v = u;
				e[m].cost = u / 2;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
				w = n + 3;
				e[m].v = u;
				e[m].cost = u / 2;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
			} else {
				w = n + 3;
				e[m].v = w;
				e[m].cost = c + u / 2 + 1;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				w = n + 4;
				e[m].v = w;
				e[m].cost = c + u / 2 + 1;
				e[m].next = adj[u];
				adj[u] = &(e[m++]);
				w = n + 2;
				e[m].v = u;
				e[m].cost = u / 2 + 1;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
				w = n + 4;
				e[m].v = u;
				e[m].cost = u / 2;
				e[m].next = adj[w];
				adj[w] = &(e[m++]);
			}
		}
	}
	
	const long long sup = 1LL << 60;
	long long dist[500001];
	min_heap h;
	data d;
	for (u = 1; u <= n; u++) dist[u] = sup;
	dist[1] = 0;
	h.size = 0;
	d.key = 0;
	d.id = 1;
	push(d, &h);
	while (h.size > 0) {
		d = pop(&h);
		u = d.id;
		if (d.key != dist[u]) continue;
		
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (dist[w] > dist[u] + p->cost) {
				dist[w] = dist[u] + p->cost;
				d.key = dist[w];
				d.id = w;
				push(d, &h);
			}
		}
	}
	if (dist[N] == sup) printf("-1\n");
	else printf("%lld\n", dist[N]);
	fflush(stdout);
	return 0;
}
0