結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 ygussanyygussany
提出日時 2020-11-27 22:33:39
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 114 ms / 3,000 ms
コード長 2,604 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 30,696 KB
実行使用メモリ 14,828 KB
最終ジャッジ日時 2023-10-09 21:33:27
合計ジャッジ時間 6,473 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,540 KB
testcase_01 AC 2 ms
8,264 KB
testcase_02 AC 92 ms
14,260 KB
testcase_03 AC 74 ms
14,196 KB
testcase_04 AC 103 ms
14,708 KB
testcase_05 AC 94 ms
12,932 KB
testcase_06 AC 87 ms
12,888 KB
testcase_07 AC 97 ms
12,852 KB
testcase_08 AC 80 ms
12,972 KB
testcase_09 AC 84 ms
13,884 KB
testcase_10 AC 77 ms
13,588 KB
testcase_11 AC 105 ms
12,812 KB
testcase_12 AC 100 ms
14,380 KB
testcase_13 AC 91 ms
12,964 KB
testcase_14 AC 89 ms
12,924 KB
testcase_15 AC 84 ms
13,836 KB
testcase_16 AC 96 ms
13,072 KB
testcase_17 AC 108 ms
14,484 KB
testcase_18 AC 94 ms
14,256 KB
testcase_19 AC 100 ms
12,836 KB
testcase_20 AC 90 ms
12,524 KB
testcase_21 AC 100 ms
12,896 KB
testcase_22 AC 82 ms
12,680 KB
testcase_23 AC 99 ms
14,448 KB
testcase_24 AC 93 ms
14,652 KB
testcase_25 AC 107 ms
13,132 KB
testcase_26 AC 92 ms
14,792 KB
testcase_27 AC 90 ms
14,564 KB
testcase_28 AC 80 ms
12,728 KB
testcase_29 AC 105 ms
14,816 KB
testcase_30 AC 114 ms
14,536 KB
testcase_31 AC 104 ms
12,968 KB
testcase_32 AC 2 ms
8,776 KB
testcase_33 AC 50 ms
13,100 KB
testcase_34 AC 102 ms
14,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct {
	long long key;
	int id;
} data;
 
typedef struct {
	data obj[200001];
	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;
}

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

int main()
{
	int i, N, M, u, w, c, d;
	list *adj[100001] = {}, e[200001], *p;
	scanf("%d %d", &N, &M);
	for (i = 0; i < M; i++) {
		scanf("%d %d %d %d", &u, &w, &c, &d);
		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].memo = d;
		e[i*2+1].memo = d;
		e[i*2].id = i * 2;
		e[i*2+1].id = i * 2 + 1;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int prev[2][100001], flag[2][100001] = {};
	long long dist[2][100001];
	for (i = 1; i <= N; i++) {
		dist[0][i] = 1LL << 60;
		dist[1][i] = 1LL << 60;
		prev[0][i] = -1;
		prev[1][i] = -1;
	}

	data da;
	min_heap h;
	h.size = 0;
	dist[0][1] = 0;
	da.key = 0;
	da.id = 1;
	push(da, &h);
	while (h.size > 0) {
		u = pop(&h).id;
		if (flag[0][u] != 0) continue;
		flag[0][u] = 0;
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (dist[0][u] + p->cost < dist[0][w]) {
				dist[0][w] = dist[0][u] + p->cost;
				prev[0][w] = p->id;
				da.key = dist[0][w];
				da.id = w;
				push(da, &h);
			}
		}
	}
	for (i = prev[0][N]; i != -1; i = prev[0][e[i^1].v]) {
		e[i].cost = e[i].memo;
		e[i^1].cost *= -1;
	}
	for (i = 0; i < M; i++) {
		u = e[i*2+1].v;
		w = e[i*2].v;
		e[i*2].cost += dist[0][u] - dist[0][w];
		e[i*2+1].cost += dist[0][w] - dist[0][u];
	}
	
	dist[1][1] = 0;
	da.key = 0;
	da.id = 1;
	push(da, &h);
	while (h.size > 0) {
		u = pop(&h).id;
		if (flag[1][u] != 0) continue;
		flag[1][u] = 1;
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (dist[1][u] + p->cost < dist[1][w]) {
				dist[1][w] = dist[1][u] + p->cost;
				prev[1][w] = p->id;
				da.key = dist[1][w];
				da.id = w;
				push(da, &h);
			}
		}
	}
	printf("%lld\n", dist[0][N] * 2 + dist[1][N]);
	fflush(stdout);
	return 0;
}
0