結果

問題 No.1577 織姫と彦星2
ユーザー kagasantwikagasantwi
提出日時 2021-06-19 15:58:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 2,411 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 88,140 KB
実行使用メモリ 5,792 KB
最終ジャッジ日時 2023-09-05 00:56:19
合計ジャッジ時間 6,258 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,388 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 10 ms
4,384 KB
testcase_06 AC 4 ms
4,384 KB
testcase_07 AC 138 ms
5,172 KB
testcase_08 AC 36 ms
4,380 KB
testcase_09 AC 211 ms
5,792 KB
testcase_10 AC 20 ms
4,384 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 63 ms
4,936 KB
testcase_13 AC 13 ms
4,388 KB
testcase_14 AC 69 ms
5,152 KB
testcase_15 AC 30 ms
4,384 KB
testcase_16 AC 127 ms
5,116 KB
testcase_17 AC 57 ms
4,536 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 104 ms
5,236 KB
testcase_20 AC 89 ms
4,996 KB
testcase_21 AC 126 ms
5,056 KB
testcase_22 AC 111 ms
5,080 KB
testcase_23 AC 37 ms
4,384 KB
testcase_24 AC 119 ms
5,660 KB
testcase_25 AC 19 ms
4,380 KB
testcase_26 AC 37 ms
4,384 KB
testcase_27 AC 36 ms
5,492 KB
testcase_28 AC 73 ms
5,084 KB
testcase_29 AC 27 ms
4,548 KB
testcase_30 AC 44 ms
4,756 KB
testcase_31 AC 102 ms
5,268 KB
testcase_32 AC 3 ms
4,384 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 31 ms
4,496 KB
testcase_35 AC 107 ms
5,052 KB
testcase_36 AC 98 ms
5,344 KB
testcase_37 AC 188 ms
5,576 KB
testcase_38 AC 167 ms
5,644 KB
testcase_39 AC 48 ms
4,380 KB
testcase_40 AC 35 ms
4,420 KB
testcase_41 AC 86 ms
5,464 KB
testcase_42 AC 30 ms
4,972 KB
testcase_43 AC 33 ms
5,188 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 16 ms
4,380 KB
testcase_46 AC 12 ms
4,384 KB
testcase_47 AC 36 ms
5,484 KB
testcase_48 AC 19 ms
4,444 KB
testcase_49 AC 50 ms
4,548 KB
testcase_50 AC 25 ms
4,384 KB
testcase_51 AC 5 ms
4,380 KB
testcase_52 AC 110 ms
5,652 KB
testcase_53 AC 124 ms
5,704 KB
testcase_54 AC 30 ms
4,704 KB
testcase_55 AC 88 ms
5,664 KB
testcase_56 AC 30 ms
4,380 KB
testcase_57 AC 12 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;

/**
 * 方針
 * 石をノードとした無向グラフとして考える。
 * ハミング距離が1以下の石の間に重み1のエッジを張る。
 * start, end間の最短距離を求める。
 * ノード数に対してエッジが少なめなことを利用する。
 */

int main(){
    
    // ローカルではテストケースを1回の実行で全部試す
    for (int N; cin >> N;) {
        // 石はstart, end, NのN+2種類
        vector<int>stone(N + 2);
        // startの石から何種類使ってその石に辿り着けたか。辿り着けない場合は-1。
        vector<int>score(N + 2, -1);
        
        // 石の性質を入力
        cin >> stone[0] >> stone[1];
        for(int i = 2; i < N + 2; i++)cin >> stone[i];
        
        // 性質の場所を記録しておく
        map<int, int>stonePosition;
        for (int i = 0; i < N + 2; i++) {
            stonePosition[stone[i]] = i;
        } 

        // 幅優先探索用キュー
        queue<int>Q;
        // 幅優先探索の初期状態
        score[0] = 0;
        Q.push(0);
        
        // 幅優先探索
        while(!Q.empty()){
            int index = Q.front();
            Q.pop();
            
            // 未利用の石を調べる
            for (int bit = 1; bit <= 1000000000; bit <<= 1) {
                // 1bitだけ異なる石の性質
                int target = stone[index] ^ bit;
                // そんな石があるかどうか
                if (stonePosition.find(target) == stonePosition.end())continue;
                int toIndex = stonePosition[target];
                
                // 繋げられない場合はcontinue
                if (__builtin_popcount(stone[index] ^ stone[toIndex]) > 1) continue;
                // 利用済みの場合はcontinue
                if (score[toIndex] >= 0) continue;
                
                // 利用する石のscoreを更新してキューに追加
                score[toIndex] = score[index] + 1;
                Q.push(toIndex);
            }
        }
        
        // 出力
        if (score[1] >= 0) {
            // end側の石は答えに含めない
            cout << score[1] - 1 << endl;    
        } else {
            cout << -1 << endl;
        }
    }
    
    return 0;
}
0