結果

問題 No.3810 Binary Shortest Path Query
コンテスト
ユーザー Rubikun
提出日時 2026-11-20 21:44:06
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 272 ms / 2,000 ms
+ 762µs
コード長 3,509 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,991 ms
コンパイル使用メモリ 245,996 KB
実行使用メモリ 31,628 KB
最終ジャッジ日時 2026-11-20 21:44:20
合計ジャッジ時間 10,698 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005;
const ll INF=15LL<<58;

struct UF{
    int n;
    vector<int> par,size,edge;
    
    void init(int n_){
        n=n_;
        par.assign(n,-1);
        size.assign(n,1);
        edge.assign(n,0);
        
        for(int i=0;i<n;i++){
            par[i]=i;
        }
    }
    
    int root(int a){
        if(par[a]==a) return a;
        else return par[a]=root(par[a]);
    }
    
    void unite(int a,int b){
        edge[root(a)]++;
        if(root(a)!=root(b)){
            size[root(a)]+=size[root(b)];
            edge[root(a)]+=edge[root(b)];
            par[root(b)]=root(a);
        }
    }
    
    bool check(int a,int b){
        return root(a)==root(b);
    }
};

vll G[MAX];
ll MI[MAX];
ll dis[MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N,M,Q;cin>>N>>M>>Q;
    vl S(N);
    for(int i=0;i<N;i++){
        cin>>S[i];
    }
    UF uf;uf.init(N);
    vector<array<ll,3>> E;
    for(ll i=1;i<=M;i++){
        ll u,v,w;cin>>u>>v>>w;u--;v--;
        if(w==0){
            uf.unite(u,v);
        }else{
            for(ll q=0;q<i;q++){
                w*=2;
                if(w>2000000000000000000LL) break;
            }
            if(w<=2000000000000000000LL){
                E.pb({u,v,w});
            }
            //cout<<u<<" "<<v<<" "<<w<<endl;
        }
    }
    
    for(int i=0;i<N;i++){
        chmin(S[uf.root(i)],S[i]);
    }
    
    for(auto [a,b,c]:E){
        if(uf.check(a,b)) continue;
        G[uf.root(a)].pb(mp(uf.root(b),c));
        G[uf.root(b)].pb(mp(uf.root(a),c));
    }
    
    for(int i=0;i<N;i++) dis[i]=INF;
    
    map<pii,ll> MA;
    for(int i=0;i<N;i++){
        if(uf.root(i)!=i) continue;
        MI[i]=S[i];
        priority_queue<pll,vector<pll>,greater<pll>> PQ;
        dis[i]=0;
        PQ.push(mp(0,i));
        
        vi miru;
        while(!PQ.empty()){
            auto [d,u]=PQ.top();PQ.pop();
            if(dis[u]<d) continue;
            
            miru.pb(u);
            
            for(auto [to,co]:G[u]){
                if(chmin(dis[to],dis[u]+co)) PQ.push(mp(dis[to],to));
            }
        }
        
        for(int x:miru){
            MA[mp(i,x)]=dis[x];
            chmin(MI[i],S[x]+dis[x]);
            //cout<<i<<" "<<x<<" "<<dis[x]<<endl;
            dis[x]=INF;
        }
        //cout<<MI[i]<<" ";
    }
    
    while(Q--){
        int a,b;cin>>a>>b;a--;b--;
        a=uf.root(a);
        b=uf.root(b);
        ll ans=INF;
        chmin(ans,MI[a]+MI[b]);
        //cout<<ans<<" ";
        if(MA.count(mp(a,b))) chmin(ans,MA[mp(a,b)]);
        cout<<ans<<"\n";
    }
}


0