結果

問題 No.1480 Many Complete Graphs
ユーザー ygussanyygussany
提出日時 2021-04-16 23:25:00
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 2,537 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 31,360 KB
実行使用メモリ 17,536 KB
最終ジャッジ日時 2024-07-03 04:19:31
合計ジャッジ時間 4,278 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,760 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 3 ms
5,760 KB
testcase_03 AC 4 ms
5,760 KB
testcase_04 AC 4 ms
5,760 KB
testcase_05 AC 3 ms
5,760 KB
testcase_06 AC 4 ms
5,760 KB
testcase_07 AC 3 ms
5,760 KB
testcase_08 AC 4 ms
5,760 KB
testcase_09 AC 4 ms
5,888 KB
testcase_10 AC 4 ms
5,888 KB
testcase_11 AC 3 ms
5,888 KB
testcase_12 AC 5 ms
5,888 KB
testcase_13 AC 10 ms
9,856 KB
testcase_14 AC 8 ms
8,832 KB
testcase_15 AC 19 ms
10,624 KB
testcase_16 AC 13 ms
8,704 KB
testcase_17 AC 12 ms
8,704 KB
testcase_18 AC 4 ms
6,784 KB
testcase_19 AC 12 ms
8,832 KB
testcase_20 AC 19 ms
10,752 KB
testcase_21 AC 12 ms
10,496 KB
testcase_22 AC 8 ms
8,448 KB
testcase_23 AC 21 ms
15,872 KB
testcase_24 AC 33 ms
15,488 KB
testcase_25 AC 42 ms
15,872 KB
testcase_26 AC 46 ms
16,256 KB
testcase_27 AC 34 ms
15,488 KB
testcase_28 AC 40 ms
16,896 KB
testcase_29 AC 38 ms
17,024 KB
testcase_30 AC 36 ms
16,640 KB
testcase_31 AC 39 ms
16,640 KB
testcase_32 AC 38 ms
16,896 KB
testcase_33 AC 39 ms
16,640 KB
testcase_34 AC 38 ms
16,896 KB
testcase_35 AC 36 ms
16,640 KB
testcase_36 AC 37 ms
16,640 KB
testcase_37 AC 38 ms
16,640 KB
testcase_38 AC 37 ms
16,640 KB
testcase_39 AC 38 ms
16,640 KB
testcase_40 AC 36 ms
16,640 KB
testcase_41 AC 36 ms
16,640 KB
testcase_42 AC 36 ms
16,896 KB
testcase_43 AC 37 ms
16,640 KB
testcase_44 AC 37 ms
16,512 KB
testcase_45 AC 38 ms
17,024 KB
testcase_46 AC 37 ms
16,640 KB
testcase_47 AC 76 ms
17,280 KB
testcase_48 AC 77 ms
17,280 KB
testcase_49 AC 75 ms
17,280 KB
testcase_50 AC 28 ms
17,024 KB
testcase_51 AC 76 ms
17,152 KB
testcase_52 AC 57 ms
17,280 KB
testcase_53 AC 58 ms
17,408 KB
testcase_54 AC 56 ms
17,152 KB
testcase_55 AC 57 ms
17,152 KB
testcase_56 AC 58 ms
17,280 KB
testcase_57 AC 30 ms
17,152 KB
testcase_58 AC 41 ms
17,536 KB
evil_aftercontest.txt AC 85 ms
29,184 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