結果

問題 No.3 ビットすごろく
ユーザー A8pfA8pf
提出日時 2018-09-15 23:29:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,311 bytes
コンパイル時間 1,172 ms
コンパイル使用メモリ 79,828 KB
実行使用メモリ 4,440 KB
最終ジャッジ日時 2023-09-14 01:06:38
合計ジャッジ時間 2,011 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 10 ms
4,380 KB
testcase_11 AC 8 ms
4,380 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 11 ms
4,440 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 11 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 9 ms
4,376 KB
testcase_23 AC 11 ms
4,376 KB
testcase_24 AC 11 ms
4,376 KB
testcase_25 AC 11 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 10 ms
4,380 KB
testcase_29 AC 7 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <functional>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class Edge
{
	public:
	int cost;
	int from;
	int to;

	Edge(int n,int c)
	{
		to=n;
		cost=c;
	}

	Edge(int f,int t,int c)
	{
		from=f;
		to=t;
		cost=c;
	}
};

vector<Edge> g[10001];
priority_queue<P,vector<P>,greater<P>> que;
bool used[10001];
int dist[10001];

int main()
{
	int n;
	int ans;
	cin>>n;
	//それぞれのマスから到達できるマスに辺を張る
	for(int i=1;i<n+1;i++)
	{
		int cnt=0;
		for(int j=0;j<16;j++)
		{
			if((i&(int)pow(2,j))>0)
				cnt++;
		}
		if(i-cnt>0)
			g[i].push_back(Edge(i-cnt,1));
		
		if(i+cnt<n+1)
			g[i].push_back(Edge(i+cnt,1));
	}
	fill(used,used+n+1,false);
	//ここからダイクストラ
	fill(dist,dist+n+1,(int)1e9-1);
	dist[1]=0;
	que.push(P(0,1));
	while(!que.empty())
	{
		P p=que.top(); que.pop();
		int v=p.second;
		if(dist[v]<p.first)
			continue;
		for(int i=0;i<g[v].size();i++)
		{
			Edge e=g[v][i];
			if(dist[e.to]>dist[v]+e.cost)
			{
				dist[e.to]=dist[v]+e.cost;
				que.push(P(dist[e.to],e.to));
			}
		}
	}
	//ここまでダイクストラ
	if(dist[n]==(int)1e9-1)
		ans=-1;
	else
		ans=dist[n]+1;
	cout<<ans<<endl;
	return 0;
}
0