結果

問題 No.1602 With Animals into Institute 2
ユーザー hiikunZhiikunZ
提出日時 2022-07-18 17:24:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,449 bytes
コンパイル時間 3,204 ms
コンパイル使用メモリ 223,448 KB
実行使用メモリ 793,800 KB
最終ジャッジ日時 2023-09-13 07:29:02
合計ジャッジ時間 12,656 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 MLE -
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//情報科学の達人で履修したので、解説ACだけどACしてみたい
//解説の写経です
#include <bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
constexpr ll MAX = 2000000000000000000;
constexpr ld PI = 3.14159265358979;
constexpr ll MOD = 0;//2024948111;
ld dotorad(ld K){return PI * K / 180.0;}
ld radtodo(ld K){return K * 180.0 / PI;}
mt19937 mt;
void randinit(){srand((unsigned)time(NULL));mt = mt19937(rand());}
int main(){
    ll N,M,K;
    cin >> N >> M >> K;
    vector<vector<ll>> G(N),C(N),D(N);
    for(ll i = 0;i < M;i++){
        ll a,b,c;
        string x;
        cin >> a >> b >> c >> x;
        a--;
        b--;
        ll d = 0;
        for(ll j = 0;j < K;j++){
            d *= 2;
            d += (x[j] == '1');
        }
        G[a].emplace_back(b);
        G[b].emplace_back(a);
        C[a].emplace_back(c);
        C[b].emplace_back(c);
        D[a].emplace_back(d);
        D[b].emplace_back(d);
    }
    vector<ll> dis(N,-1),lav(N,-1),par(N,-1),depth(N,-1);
    {
        //dis,lav,idx,par,depth
        priority_queue<tuple<ll,ll,ll,ll,ll>,vector<tuple<ll,ll,ll,ll,ll>>,greater<tuple<ll,ll,ll,ll,ll>>> que;
        que.emplace(0,0,N - 1,-1,0);
        while(!que.empty()){
            ll d = get<0>(que.top());
            ll l = get<1>(que.top());
            ll a = get<2>(que.top());
            ll p = get<3>(que.top());
            ll dep = get<4>(que.top());
            que.pop();
            if(dis[a] == -1){
                dis[a] = d;
                lav[a] = l;
                par[a] = p;
                depth[a] = dep;
                for(ll i = 0;i < (ll)G[a].size();i++){
                    que.emplace(d + C[a][i],l ^ D[a][i],G[a][i],a,dep + 1);
                }
            }
        }
    }
    vector<ll> root(N),ans(N,MAX);
    for(ll i = 0;i < N;i++){
        root[i] = i;
    }
    priority_queue<tuple<ll,ll,ll>,vector<tuple<ll,ll,ll>>,greater<tuple<ll,ll,ll>>> que;
    for(ll i = 0;i < N;i++){
        for(ll j = 0;j < (ll)G[i].size();j++){
            ll u = i,v = G[i][j];
            ll l = lav[u] ^ lav[v] ^ D[i][j];
            if(l != 0){
                ll h = dis[u] + dis[v] + C[i][j];
                que.emplace(h,u,v);
            }
        }
    }
    while(!que.empty()){
        ll h = get<0>(que.top());
        ll u = get<1>(que.top());
        ll v = get<2>(que.top());
        que.pop();
        u = root[u];
        v = root[v];
        vector<ll> B;
        while(u != v){
            if(depth[u] > depth[v]){
                B.emplace_back(u);
                u = root[par[u]];
            }
            else{
                B.emplace_back(v);
                v = root[par[v]];
            }
        }
        for(ll i = 0;i < (ll)B.size();i++){
            ll x = B[i];
            root[x] = u;
            ans[x] = h - dis[x];
            for(ll j = 0;j < (ll)G[x].size();j++){
                ll p = x,q = G[x][j];
                ll l = lav[p] ^ lav[q] ^ D[x][j];
                if(l == 0){
                    ll h = ans[p] + dis[q] + C[x][j];
                    que.emplace(h,p,q);
                }
            }
        }
    }
    for(ll i = 0;i < N;i++) if(lav[i] != 0) ans[i] = dis[i];
    for(ll i = 0;i < N - 1;i++){
        if(ans[i] == MAX) cout << -1 << endl;
        else cout << ans[i] << endl;
    }
}
0