結果

問題 No.1868 Teleporting Cyanmond
ユーザー Kome_soudouKome_soudou
提出日時 2022-03-12 19:19:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 800 bytes
コンパイル時間 1,780 ms
コンパイル使用メモリ 177,036 KB
実行使用メモリ 9,040 KB
最終ジャッジ日時 2023-10-17 04:38:13
合計ジャッジ時間 5,427 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 38 ms
8,248 KB
testcase_04 AC 28 ms
6,928 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 7 ms
4,348 KB
testcase_07 AC 20 ms
5,732 KB
testcase_08 AC 12 ms
4,816 KB
testcase_09 AC 17 ms
5,468 KB
testcase_10 AC 36 ms
7,720 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 10 ms
4,552 KB
testcase_13 AC 20 ms
5,872 KB
testcase_14 AC 41 ms
8,512 KB
testcase_15 AC 27 ms
6,968 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 10 ms
4,348 KB
testcase_18 AC 44 ms
9,040 KB
testcase_19 AC 45 ms
9,040 KB
testcase_20 AC 44 ms
9,040 KB
testcase_21 AC 44 ms
9,040 KB
testcase_22 AC 44 ms
9,040 KB
testcase_23 AC 44 ms
9,040 KB
testcase_24 AC 44 ms
9,040 KB
testcase_25 AC 43 ms
9,040 KB
testcase_26 AC 43 ms
9,040 KB
testcase_27 AC 43 ms
9,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Edge
{
	int to; // 辺の行先
	int weight; // 辺の重み
	Edge(int t, int w) : to(t), weight(w){} 
};
using Graph = vector<vector<Edge>>;

int main()
{
	int N;
	cin >> N;
	int R;
	Graph G(N);
	for(int i = 0; i < N - 1; i++)
	{
		cin >> R;
		R--;
		G[i].push_back(Edge(R, 1));
	}
	for(int i = 0; i < N - 1; i++) G[i + 1].push_back(Edge(i, 0));
	
	int inf = 100000001;
	deque<int> dq;
	dq.push_back(0);
	vector<int> dist(N, inf);
	dist[0] = 0;
	while(!dq.empty())
	{
		int p = dq.front();
		dq.pop_front();
		
		for(Edge e : G[p])
		{
			if(dist[p] + e.weight < dist[e.to])
			{
				dist[e.to] = dist[p] + e.weight;
				if(e.weight == 0) dq.push_front(e.to);
				else dq.push_back(e.to);
			}
		}
	}
	
	cout << dist[N - 1] << endl;
	return 0;
}
0