結果

問題 No.1577 織姫と彦星2
ユーザー rogi52rogi52
提出日時 2022-10-22 13:14:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,330 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 211,188 KB
実行使用メモリ 8,756 KB
最終ジャッジ日時 2023-09-14 08:03:47
合計ジャッジ時間 6,639 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

vector<int> bfs(vector<vector<int>> &graph, int s) {
    int INF = numeric_limits<int>::max();
    vector<int> dist(graph.size(), INF);
    queue<int> q;
    dist[s] = 0, q.push(s);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int v : graph[u]) if(dist[v] == INF)
            dist[v] = dist[u] + 1, q.push(v);
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,start,end; cin >> N >> start >> end;
    vector<int> stone(N);
    rep(i,N) cin >> stone[i];
    stone.push_back(start);
    stone.push_back(end);
    N += 2;

    vector<int> V = stone;
    sort(V.begin(), V.end());
    vector<vector<int>> G(N);
    rep(i,N)rep(j,i) {
        int u = lower_bound(V.begin(), V.end(), stone[i]) - V.begin();
        int v = lower_bound(V.begin(), V.end(), stone[j]) - V.begin();
        if(__builtin_popcount(stone[i] ^ stone[j]) == 1) {
            G[u].push_back(v);
            G[v].push_back(u);
        }
    }

    start = lower_bound(V.begin(), V.end(), start) - V.begin();
    end = lower_bound(V.begin(), V.end(), end) - V.begin();
    int ans = bfs(G, start)[end];
    cout << (ans == numeric_limits<int>::max() ? -1 : ans - 1) << endl;
}
0