結果

問題 No.1576 織姫と彦星
ユーザー kagasantwikagasantwi
提出日時 2021-06-19 15:26:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,798 bytes
コンパイル時間 1,219 ms
コンパイル使用メモリ 76,088 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-05 00:54:20
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 WA -
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 WA -
testcase_20 AC 2 ms
4,376 KB
testcase_21 WA -
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,500 KB
testcase_24 WA -
testcase_25 AC 2 ms
4,376 KB
testcase_26 WA -
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 3 ms
4,380 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 3 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 2 ms
4,380 KB
testcase_46 WA -
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 1 ms
4,500 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 4 ms
4,380 KB
testcase_54 AC 3 ms
4,376 KB
testcase_55 AC 3 ms
4,380 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 WA -
testcase_58 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
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];
        
        // 幅優先探索用キュー
        queue<int>Q;
        // 幅優先探索の初期状態
        score[0] = 0;
        Q.push(0);
        
        // 幅優先探索
        while(!Q.empty()){
            int index = Q.front();
            Q.pop();
            
            // 未利用の石を調べる
            for (int i = 0; i < N + 1; i++) {
                // 繋げられない場合はcontinue
                if (__builtin_popcount(stone[index] ^ stone[i]) > 1) continue;
                // 利用済みの場合はcontinue
                if (score[i] >= 0) continue;
                
                // 利用する石のscoreを更新してキューに追加
                score[i] = score[index] + 1;
                Q.push(i);
            }
        }
        
        // 出力
        if (score[1] >= 0) {
            // end側の石は答えに含めない
            cout << score[1] - 1 << endl;    
        } else {
            cout << -1 << endl;
        }
    }
    
    return 0;
}
0