結果

問題 No.1602 With Animals into Institute 2
ユーザー hiikunZhiikunZ
提出日時 2022-07-19 08:56:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 859 ms / 4,000 ms
コード長 3,876 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 225,608 KB
実行使用メモリ 53,296 KB
最終ジャッジ日時 2023-09-14 03:00:13
合計ジャッジ時間 18,215 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 7 ms
4,396 KB
testcase_04 AC 7 ms
4,376 KB
testcase_05 AC 9 ms
4,572 KB
testcase_06 AC 605 ms
50,724 KB
testcase_07 AC 607 ms
51,016 KB
testcase_08 AC 826 ms
50,552 KB
testcase_09 AC 845 ms
52,796 KB
testcase_10 AC 842 ms
52,664 KB
testcase_11 AC 850 ms
52,628 KB
testcase_12 AC 859 ms
52,688 KB
testcase_13 AC 842 ms
52,700 KB
testcase_14 AC 843 ms
52,628 KB
testcase_15 AC 840 ms
53,108 KB
testcase_16 AC 837 ms
53,296 KB
testcase_17 AC 839 ms
53,008 KB
testcase_18 AC 9 ms
4,752 KB
testcase_19 AC 9 ms
4,612 KB
testcase_20 AC 9 ms
4,540 KB
testcase_21 AC 9 ms
4,644 KB
testcase_22 AC 9 ms
4,636 KB
testcase_23 AC 9 ms
4,540 KB
testcase_24 AC 9 ms
4,676 KB
testcase_25 AC 10 ms
4,800 KB
testcase_26 AC 9 ms
4,668 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,504 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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());}
ll get_root(ll x,vector<ll> &root){
    if(root[x] == x) return x;
    return root[x] = get_root(root[x],root);
}
int main(){
    ll N,M,K;
    scanf("%lld %lld %lld",&N,&M,&K);
    vector<vector<ll>> G(N),C(N),D(N),H(N);
    for(ll i = 0;i < M;i++){
        int a,b,c;
        char ch[K + 1];
        scanf("%d %d %d %s",&a,&b,&c,ch);
        a--;
        b--;
        ll d = 0;
        for(ll j = 0;j < K;j++){
            d *= 2;
            d += (ch[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);
        H[a].emplace_back(MAX);
        H[b].emplace_back(MAX);
    }
    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];
                H[i][j] = h;
                que.emplace(h,i,j);
            }
        }
    }
    while(!que.empty()){
        ll h = get<0>(que.top());
        ll u = get<1>(que.top());
        ll I = get<2>(que.top());
        ll v = G[u][I];
        que.pop();
        u = get_root(u,root);
        v = get_root(v,root);
        vector<ll> B;
        while(u != v){
            if(depth[u] > depth[v]){
                B.emplace_back(u);
                u = get_root(par[u],root);
            }
            else{
                B.emplace_back(v);
                v = get_root(par[v],root);
            }
        }
        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];
                    if(h < H[x][j]){
                        H[x][j] = h;
                        que.emplace(h,x,j);
                    }
                }
            }
        }
    }
    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) printf("-1\n");
        else printf("%lld\n",ans[i]);
    }
}
0