結果

問題 No.1868 Teleporting Cyanmond
ユーザー Kome_soudou
提出日時 2022-03-12 19:19:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 800 bytes
コンパイル時間 1,909 ms
コンパイル使用メモリ 176,140 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2024-09-17 03:21:57
合計ジャッジ時間 3,753 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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