結果

問題 No.3 ビットすごろく
ユーザー sugim48sugim48
提出日時 2014-12-21 12:17:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,478 bytes
コンパイル時間 1,298 ms
コンパイル使用メモリ 83,780 KB
実行使用メモリ 4,560 KB
最終ジャッジ日時 2023-09-02 20:41:34
合計ジャッジ時間 2,676 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

vector<edge> G[10001];
ll d[10001];

void dijkstra_deque(int n, vector<edge> G[], int s, ll d[]) {
	vector<bool> vis(n, false);
	fill(d, d + n, LLONG_MAX); d[s] = 0;
	deque<ll> deq; deq.push_back(s);
	while (!deq.empty()) {
		int u = deq.front(); deq.pop_front();
		if (vis[u]) continue;
		vis[u] = true;
		for (int i = 0; i < G[u].size(); i++) {
			edge e = G[u][i];
			if (d[e.v] > d[u] + e.w) {
				d[e.v] = d[u] + e.w;
				if (e.w == 0) deq.push_front(e.v);
				else deq.push_back(e.v);
			}
		}
	}
}

void add_edge(int u, int v) {
	edge e = {u, v, 1};
	G[u].push_back(e);
}

int main() {
	int N; cin >> N;
	for (int i = 1; i <= N; i++) {
		int x = __builtin_popcount(i);
		if (i - x >= 0) add_edge(i, i - x);
		if (i + x <= N) add_edge(i, i + x);
	}
	dijkstra_deque(N + 1, G, 1, d);
	cout << (d[N] < LLONG_MAX ? d[N] : -1) << endl;
}
0