結果

問題 No.1602 With Animals into Institute 2
ユーザー bin101bin101
提出日時 2022-04-13 01:39:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 861 ms / 4,000 ms
コード長 4,320 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 189,864 KB
実行使用メモリ 52,160 KB
最終ジャッジ日時 2023-08-23 11:57:19
合計ジャッジ時間 15,769 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 9 ms
4,380 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 11 ms
4,604 KB
testcase_06 AC 678 ms
37,168 KB
testcase_07 AC 656 ms
37,156 KB
testcase_08 AC 861 ms
45,736 KB
testcase_09 AC 852 ms
50,980 KB
testcase_10 AC 823 ms
51,104 KB
testcase_11 AC 848 ms
50,884 KB
testcase_12 AC 788 ms
51,732 KB
testcase_13 AC 794 ms
52,160 KB
testcase_14 AC 835 ms
50,744 KB
testcase_15 AC 730 ms
51,792 KB
testcase_16 AC 749 ms
51,552 KB
testcase_17 AC 750 ms
51,292 KB
testcase_18 AC 11 ms
4,760 KB
testcase_19 AC 12 ms
4,920 KB
testcase_20 AC 12 ms
4,708 KB
testcase_21 AC 11 ms
4,404 KB
testcase_22 AC 11 ms
4,564 KB
testcase_23 AC 11 ms
4,408 KB
testcase_24 AC 11 ms
4,608 KB
testcase_25 AC 10 ms
4,376 KB
testcase_26 AC 10 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 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,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//経路圧縮なし
//パラレル3本以上あり
//
#include<bits/stdc++.h> 
using namespace std;
using ll=long long int;
#define int ll

//T: 距離 S: ラベル(群) U: 群の演算
template<typename T,typename S,typename U>
struct ShortestNonzeroPath{
    struct edge{
        int from;
        int to;
        T cost;
        S label;
        int idx;
        //edge(){}
        bool operator<(edge e)const{
            return from<e.from;
        }

    };

    
    int V; //頂点数
    T inf;
    vector<T> q; //shortest unorthodox path distance
    vector<int> parent;
    S identity;
    int s; //始点
    vector<T> dist;
    vector<int> depth;
    vector<S> psi;
    vector<vector<edge>> G;
    vector<int> pred;


    priority_queue<pair<T,edge>,vector<pair<T,edge>>,greater<>> F;
    vector<T> h;
    vector<T> ans;
    int E; //辺数
    U f;
    void add_edge(int from,int to,T cost,S label){
        G[from].push_back({from,to,cost,label,E});
        E++;
    }
    bool is_consistent(edge e){
        if(f(psi[e.from],e.label)==psi[e.to]) return true;
        return false;
    }


    ShortestNonzeroPath(int V,int identity,int s,U f):V(V),inf(numeric_limits< T >::max()),q(V,inf),
        identity(identity),s(s),dist(V,inf),depth(V,inf),psi(V,identity),
        G(V),pred(V),ans(V),E(0),f(f){

    }
    void build(){
        h.resize(E,inf);
        Dijkstra();
        Step1();
        Step2();
        while(F.size()) Step3();
        ans.resize(V);
        for(int i=0;i<V;i++){
            if(psi[i]==identity) ans[i]=q[i];
            else ans[i]=dist[i];
        }
    }


    //始点: 0
    void Dijkstra(){
        using pti=pair<T,int>;
        priority_queue<pti,vector<pti>,greater<pti>> que;
        dist[s]=0;
        psi[s]=identity;
        depth[s]=0;
        que.emplace(dist[s],s);

        while(!que.empty()){
            T cost;
            int idx;
            tie(cost,idx)=que.top();
            que.pop();
            if(dist[idx]<cost) continue;
            for(auto &e:G[idx]){
                auto next_cost=cost+e.cost;
                if(dist[e.to]<=next_cost) continue;
                pred[e.to]=idx;
                depth[e.to]=depth[idx]+1;
                psi[e.to]=f(psi[idx],e.label);
                dist[e.to]=next_cost;
                que.emplace(dist[e.to],e.to);
            }
        }
    }

    void Step1(){
        parent.resize(V);
        for(int v=0;v<V;v++) parent[v]=v;
    }

    void Step2(){
        for(int v=0;v<V;v++){
            for(auto e:G[v]){
                if(is_consistent(e)) h[e.idx]=inf;
                else{
                    h[e.idx]=dist[v]+dist[e.to]+e.cost;
                    F.emplace(h[e.idx],e);
                }
            }
        }
    }
    int root(int  x){
        if(x==parent[x]) return x;
        else return root(parent[x]);
    }
    void Step3(){
//3_1 start
        T delta;
        edge e;
        tie(delta,e)=F.top();
        F.pop();
        if(delta!=h[e.idx]) return;
        array<int,2> w;
        w[0]=root(e.from);
        w[1]=root(e.to);
        vector<int> B;
//3_1 end
        while(w[0]!=w[1]){
            if(depth[w[0]]<depth[w[1]]) swap(w[0],w[1]); //0の方を深くする 0を親に移動する
            B.push_back(w[0]);
            w[0]=root(pred[w[0]]);
        }
//3_2 end
        int w_1=w[0];
        for(int w:B){
            parent[w]=w_1;
            q[w]=h[e.idx]-dist[w];
            for(auto e:G[w]){
                if(not is_consistent(e)) continue;
                T cost=q[w]+dist[e.to]+e.cost;
                if(h[e.idx]>cost){
                    h[e.idx]=cost;
                    F.emplace(cost,e);
                }
            }
        }
    }

};


signed main(){
    int N,M,K;
    cin>>N>>M>>K;
    auto f=[](int a,int b){return a^b;};
    ShortestNonzeroPath<ll,int,decltype(f)> S(N,0,N-1,f);

    while(M--){
        int A,B,C;
        string X;
        cin>>A>>B>>C>>X;
        A--; B--;
        int x=0,x_=0;
        for(int i=0;i<K;i++){
            if(X[i]=='1') x+=(1<<i);
            else x_+=(1<<i);
        }
        S.add_edge(A,B,C,x);
        S.add_edge(B,A,C,x);
    }
    S.build();
    for(auto &i:S.ans) if(i==S.inf) i=-1;
    for(int i=0;i<N-1;i++){
        cout<<S.ans[i]<<endl;
    }
    //cerr<<S.parent[2]<<endl;
}
0