結果
| 問題 | No.3810 Binary Shortest Path Query |
| コンテスト | |
| ユーザー |
ryoku
|
| 提出日時 | 2026-11-20 23:02:58 |
| 言語 | C++23(gnu拡張gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
WA
不安定
|
| 実行時間 | - |
| コード長 | 19,777 bytes |
| 記録 | |
| コンパイル時間 | 5,368 ms |
| コンパイル使用メモリ | 402,572 KB |
| 実行使用メモリ | 46,340 KB |
| 最終ジャッジ日時 | 2026-11-20 23:03:22 |
| 合計ジャッジ時間 | 21,800 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / judge4_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 2 |
| other | WA * 30 |
ソースコード
// library begin: "lib/template.hpp"
//https://github.com/ryoku1020/lib
#ifdef TEMPLATE
#else
#define TEMPLATE
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#include<cassert>
using namespace std;
namespace ryoku{}
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
using vi=vc<int>;
using vvi=vvc<int>;
using vl=vc<ll>;
using vvl=vvc<ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=ll(n);i>=ll(m);i--)
#define drep(i,n) for(ll i=(ll(n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp(...) make_pair(__VA_ARGS__)
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
istream& operator>>(istream&is,i128&x){
string s;if(!(is>>s))return is;
u128 y=0;
int i=0,neg=0;
if(s[0]=='-'||s[0]=='+')neg=s[0]=='-',i=1;
for(;i<(int)s.size();i++)y=y*10+s[i]-'0';
x=neg?(y?-(i128)(y-1)-1:0):(i128)y;
return is;
}
ostream& operator<<(ostream&os,i128 x){
if(x==0)return os<<0;
if(x<0)os<<"-";
u128 y=x<0?-(u128)x:(u128)x;
string s;
while(y)s.pb('0'+y%10),y/=10;
reverse(all(s));
return os<<s;
}
void scan(int&a){cin>>a;}
void scan(ll&a){cin>>a;}
void scan(i128&a){cin>>a;}
void scan(string&a){cin>>a;}
void scan(char&a){cin>>a;}
void scan(uint&a){cin>>a;}
void scan(ull&a){cin>>a;}
void scan(bool&a){cin>>a;}
void scan(ld&a){ cin>>a;}
template<class A,class B>
void scan(pair<A,B>&p){
scan(p.first);
scan(p.second);
}
template<class C>void scan(C&a){for(auto&x:a)scan(x);}
void read(){}
template<class Head,class... Tail>void read(Head&head,Tail&... tail){scan(head); read(tail...);}
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type,name,...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type,name,size,...) vector<vector<type>> name(size,vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a){cout<<a;}
template<class T>void print(vector<T>a){for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}}
void PRT(){cout<<endl; return;}
template<class T>void PRT(T a){print(a); cout<<endl; return;}
template<class Head,class... Tail>void PRT(Head head,Tail... tail){print(head); cout<<" "; PRT(tail...); return;}
template<class T,class F>int chmin(T&x,F y){return x>y?(x=y,1):0;}
template<class T,class F>int chmax(T&x,F y){return x<y?(x=y,1):0;}
template<typename T>T floor(T a,T b){return a/b-(a%b&&((a<0)!=(b<0)));}
template<typename T>T ceil(T a,T b){return a/b+(a%b&&((a<0)==(b<0)));}
template<typename T>T bmod(T x,T y){return x-y*floor(x,y);}
template<typename T>
pair<T,T>divmod(T x,T y){
T q=floor(x,y);
return{q,x-q*y};
}
void YesNo(bool b){cout<<(b?"Yes":"No")<<endl;}void YESNO(bool b){cout<<(b?"YES":"NO")<<endl;}
void AliceBob(bool b){cout<<(b?"Alice":"Bob")<<endl;}
void Yes(){cout<<"Yes"<<endl;}void YES(){cout<<"YES"<<endl;}
void No(){cout<<"No"<<endl;}void NO(){cout<<"NO"<<endl;}
#ifdef LOCAL
#define LOJ(a, b) (a)
#else
#define LOJ(a, b) (b)
#endif
vc<int>stovi(const string&s,const string&S){
vc<int>v(s.size());
rep(i,s.size()){
auto t=S.find(s[i]);
assert(t!=string::npos);
v[i]=t;
}
return v;
}
template<class T=ll>
T isqrt(T x){
T F=sqrtl(x);
if(F*F>x)F--;
return F;
}
template<class C>C sorted(C a){
sort(all(a));
return a;
}
template<class C>C reved(C a){
reverse(all(a));
return a;
}
template<class T>
pair<vc<int>,vc<T>>compress(const vc<T>&a){
vc<T>xs=a;
sort(all(xs));
xs.erase(unique(all(xs)),xs.end());
vc<int>b(a.size());
rep(i,a.size())b[i]=lower_bound(all(xs),a[i])-xs.begin();
return{b,xs};
}
template<class T>
auto make_vec(int n,T x){
return vc<T>(n,x);
}
template<class... Args>
auto make_vec(int n,Args...args){
return vc(n,make_vec(args...));
}
template<class T>T tri(T x){return x*(x-1)/2;}
//[l,r)
template<class T>T tri(T l,T r){return (r-l)*(l+r-1)/2;}
//n を先頭に持ってくる
template<class C>
C rot(C v,int n){
rotate(v.begin(),v.begin()+n,v.end());
return v;
}
template<class C>
C concat(C a,const C&b){
a.insert(a.end(),b.begin(),b.end());
return a;
}
template<class Container>
auto rle(const Container&cv){
using T=typename Container::value_type;
vc<pair<T,int>>res;
for(const auto&x:cv){
if(res.empty()||res.back().fi!=x)res.pb({x,1});
else res.back().se++;
}
return res;
}
template<class T>
vc<T>iota(int n){
vc<T>v(n);rep(i,n)v[i]=i;
return v;
}
template<class T,int minfirst,class C>
vc<int>argsort(const C&a){
auto idx=iota<int>(a.size());
sort(all(idx),[&](int i,int j){
return (minfirst?make_pair(a[i],i)<make_pair(a[j],j):make_pair(a[i],i)>make_pair(a[j],j));
});
return idx;
}
template<class C>
auto trans(const C&a){
using T=typename C::value_type::value_type;
assert(a.size()&&a[0].size());
rep(i,a.size())assert(a[i].size()==a[0].size());
vvc<T>b(a[0].size(),vc<T>(a.size()));
rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
return b;
}
vc<string>trans(const vc<string>&a){
assert(a.size()&&a[0].size());
rep(i,a.size())assert(a[i].size()==a[0].size());
vc<string>b(a[0].size(),string(a.size(),0));
rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
return b;
}
template<class C>ll sz(const C&a){return (int)a.size();}
template<class C,class T>auto lb(const C&a,const T&x){return lower_bound(all(a),x);}
template<class C,class T>auto ub(const C&a,const T&x){return upper_bound(all(a),x);}
template<class T>int popcount(T n){return __builtin_popcountll(n);}
template<class T=void,class L=ll,class C>L sum(const C&a){return accumulate(all(a),L(0));}
template<class T>
struct subset_view{
T s;
struct iterator{
T s,x;
bool done;
T operator*()const{return x;}
iterator&operator++(){
if(x==0)done=true;
else x=(x-1)&s;
return*this;
}
bool operator!=(const iterator&r)const{return done!=r.done;}
};
iterator begin()const{return{s,s,false};}
iterator end()const{return{s,0,true};}
};
template<class T>
subset_view<T>subset(T s){return{s};}
template<class C>auto max(const C&a){return *max_element(all(a));}
template<class C>auto min(const C&a){return *min_element(all(a));}
template<class C>ll argmin(const C&a){return min_element(all(a))-a.begin();}
template<class C>ll argmax(const C&a){return max_element(all(a))-a.begin();}
template<class L=ll,class C>
vc<L>presum(const C&a){
vc<L>ret(a.size()+1);
rep(i,a.size())ret[i+1]=ret[i]+a[i];
return ret;
}
template<class T,class F>
vc<T>&operator+=(vc<T>&a,F b){
for(auto&v:a)v+=b;
return a;
}
template<class T,class F>
vc<T>&operator-=(vc<T>&a,F b){
for(auto&v:a)v-=b;
return a;
}
template<class T,class F>
vc<T>&operator*=(vc<T>&a,F b){
for(auto&v:a)v*=b;
return a;
}
template<class T,size_t N,class F>
array<T,N>&operator+=(array<T,N>&a,F b){
for(auto&v:a)v+=b;
return a;
}
template<class T,size_t N,class F>
array<T,N>&operator-=(array<T,N>&a,F b){
for(auto&v:a)v-=b;
return a;
}
template<class T,size_t N,class F>
array<T,N>&operator*=(array<T,N>&a,F b){
for(auto&v:a)v*=b;
return a;
}
template<class T=ll>
constexpr T pow(T a,T b){
T res=1;
while(b){
if(b&1)res*=a;
a*=a;
b/=2;
}
return res;
}
template<class T>
struct temporary{
T& x;T old;
template<class U>temporary(T&x,U&&y):x(x),old(x){
x=std::forward<U>(y);
}
~temporary(){x=std::move(old);}
temporary(const temporary&)=delete;
};
template<class T,class U>
temporary(T&,U&&)->temporary<T>;
#define CAT_(a,b) a##b
#define CAT(a,b) CAT_(a,b)
#define WITH(x,y) temporary CAT(_tmp_,__COUNTER__)(x,y)
constexpr ll two(ll a){return (1ll<<a);}
constexpr ll ten(ll a){return pow<ll>(10,a);}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<>constexpr pll inf<pll> ={inf<ll>,inf<ll>};
template<class T>constexpr T neg_inf=-inf<T>;
template<>constexpr pll neg_inf<pll> ={-inf<ll>,-inf<ll>};
template<class C>C UNIQUE(C&a){
sort(all(a));
a.erase(unique(all(a)),a.end());
return a;
}
template<class T>
int tbit(T x){
using U=make_unsigned_t<T>;
U y=(U)x;
if(!y)return -1;
if(sizeof(U)>sizeof(ull)&&(u128)y>>64)return 127-__builtin_clzll((ull)((u128)y>>64));
return 63-__builtin_clzll((ull)y);
}
template<class T>
int lbit(T x){
using U=make_unsigned_t<T>;
U y=(U)x;
if(!y)return -1;
if((ull)y)return __builtin_ctzll((ull)y);
return 64+__builtin_ctzll((ull)((u128)y>>64));
}
template<class T>
int tbit(T x,int p){
using U=make_unsigned_t<T>;
constexpr int W=numeric_limits<U>::digits;
U y=(U)x;
if(p<0)return -1;
if(p>=W-1)return tbit(y);
return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
using U=make_unsigned_t<T>;
constexpr int W=numeric_limits<U>::digits;
U y=(U)x;
if(p<0)return lbit(y);
if(p>=W)return -1;
return lbit(y&(~U(0)<<p));
}
enum debug_color{
BLACK=30,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE,
BRIGHT_BLACK=90,BRIGHT_RED,BRIGHT_GREEN,BRIGHT_YELLOW,BRIGHT_BLUE,BRIGHT_MAGENTA,BRIGHT_CYAN,BRIGHT_WHITE
};
static int dbging=1;
#ifdef LOCAL
#include"lib/debug/local.hpp"
#define mark(col) do{if(dbging!=0)::debug::internal::mark(col);}while(0)
#else
template<debug_color col,class... Ts>
void dbg(const Ts&...xs){}
#define dbg(...) ((void)0)
#define mark(...) ((void)0)
#endif
struct template_setup{
template_setup(){
#if defined(LOCAL)&&!defined(INTERACTIVE)&&!defined(SAMPLE_TEST)
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
cout<<fixed<<setprecision(6);
#else
cout<<fixed<<setprecision(20);
#endif
}
};
static struct template_setup template_setup;
#endif
// library end: "lib/template.hpp"
// library begin: "lib/ds/union_find/uf.hpp"
// library include: "lib/template.hpp"
namespace ryoku{
template<class T,auto op,int extra>
struct base_disjoint_set_union{
vector<int>par;
vector<T>data;
base_disjoint_set_union(int n){
static_assert(!extra,"e is needed");
assert(n>=0);
par.assign(n,-1);
}
base_disjoint_set_union(int n,T e){
assert(n>=0);
par.assign(n,-1);
data.assign(n,e);
}
T& operator[](int i){
static_assert(extra,"no data");
return data[leader(i)];
}
int root(int x){
assert(0<=x&&x<par.size());
if(par[x]<0)return x;
else return par[x]=root(par[x]);
}
bool same(int x,int y){
assert(0<=x&&x<par.size()&&0<=y&&y<par.size());
return root(x)==root(y);
}
bool merge(int x,int y){
assert(0<=x&&x<par.size()&&0<=y&&y<par.size());
x=root(x);
y=root(y);
if(x==y)return false;
if(par[x]>par[y])swap(x,y);
par[x]+=par[y];
par[y]=x;
if constexpr(extra){
data[x]=op(data[y],data[x]);
}
return true;
}
int size(int x){
assert(0<=x&&x<par.size());
return -par[root(x)];
}
int leader(int x){
assert(0<=x&&x<par.size());
return root(x);
}
};
int tf323(int a,int b){return a;}
using disjoint_set_union=base_disjoint_set_union<int,tf323,0>;
template<class T,auto op>
using extra_disjoint_set_union=base_disjoint_set_union<T,op,1>;
}
#ifdef LOCAL
#include"lib/debug/formatters/ds--union_find--uf.hpp"
#endif
// library end: "lib/ds/union_find/uf.hpp"
// library include: "lib/ds/union_find/uf.hpp"
// library begin: "lib/graph/base.hpp"
// library include: "lib/template.hpp"
namespace ryoku{
struct unweighted{
unweighted()=default;
unweighted(int){}
operator int()const{return 1;}
};
template<class T=unweighted>
struct edge{
int from,to,id;
[[no_unique_address]]T cost;
#ifdef LOCAL
friend ostream&operator<<(ostream&os,const edge&e){
return os<<"{from:"<<e.from<<",to:"<<e.to<<",id:"<<e.id<<",cost:"<<e.cost<<"}";
}
#endif
};
template<bool is_directed,class T=unweighted>
struct static_graph{
constexpr static bool directed(){return is_directed;}
using edge=::ryoku::edge<T>;
using cost_t=T;
private:
int n;
mutable bool built=false,inv_built=false;
vc<edge>edges;
mutable vc<int>start,inv_start;
mutable vc<edge>csr,inv_csr;
public:
static_graph(int n):n(n),start(n+1),inv_start(n+1){assert(n>=0);}
static_graph(int n,int m):static_graph(n){assert(m>=0);edges.reserve(m);}
void resize(int size){
assert(n<=size&&!built);
assert(!inv_built);
n=size;
start.resize(n+1);
inv_start.resize(n+1);
}
void add_edge(const edge&e){
assert(!built&&!inv_built);
assert(0<=e.from&&e.from<n&&0<=e.to&&e.to<n);
edges.pb(e);
}
void add_edge(int a,int b,cost_t cost=1,int id=-1){
assert(!built&&!inv_built);
assert(0<=a&&a<n&&0<=b&&b<n);
if(id==-1)id=edges.size();
edges.pb({a,b,id,cost});
}
template<int substract=0>
void input(int m){
assert(m>=0);
rep(i,m){
INT(a,b);
a-=substract;b-=substract;
add_edge(a,b);
}
build();
}
void build()const{
if(built)return;
built=true;
start.assign(n+1,0);
for(auto&e:edges){
++start[e.from];
if constexpr(!is_directed)++start[e.to];
}
rep(i,n)start[i+1]+=start[i];
csr.resize(start[n]);
for(auto it=edges.rbegin();it!=edges.rend();++it){
auto&e=*it;
csr[--start[e.from]]=e;
if constexpr(!is_directed)csr[--start[e.to]]={e.to,e.from,e.id,e.cost};
}
}
void buildinv()const{
if(inv_built)return;
inv_start.assign(n+1,0);
for(auto&e:edges){
++inv_start[e.to];
if constexpr(!is_directed)++inv_start[e.from];
}
rep(i,n)inv_start[i+1]+=inv_start[i];
inv_csr.resize(inv_start[n]);
for(auto it=edges.rbegin();it!=edges.rend();++it){
auto&e=*it;
inv_csr[--inv_start[e.to]]={e.to,e.from,e.id,e.cost};
if constexpr(!is_directed)inv_csr[--inv_start[e.from]]={e.from,e.to,e.id,e.cost};
}
inv_built=true;
}
auto operator[](int u){
build();
assert(0<=u&&u<n);
return span<edge>(csr.data()+start[u],start[u+1]-start[u]);
}
auto operator[](int u)const{
build();
assert(0<=u&&u<n);
return span<const edge>(csr.data()+start[u],start[u+1]-start[u]);
}
auto inv(int u){
buildinv();
assert(0<=u&&u<n);
return span<edge>(inv_csr.data()+inv_start[u],inv_start[u+1]-inv_start[u]);
}
auto inv(int u)const{
buildinv();
assert(0<=u&&u<n);
return span<const edge>(inv_csr.data()+inv_start[u],inv_start[u+1]-inv_start[u]);
}
const vc<edge>&all_edges()const{return edges;}
int edge_size()const{return edges.size();}
edge get_edge(int id)const{
assert(0<=id&&id<edge_size());
return edges[id];
}
int out_deg(int u)const{
build();
assert(0<=u&&u<n);
return start[u+1]-start[u];
}
int in_deg(int u)const{
buildinv();
assert(0<=u&&u<n);
return inv_start[u+1]-inv_start[u];
}
int deg(int u)const{return out_deg(u);}
int size()const{return n;}
template<class F>
vvc<F>adj()const{
vvc<F>res(n,vc<F>(n));
for(auto&e:edges){
res[e.from][e.to]=e.cost;
if constexpr(!is_directed)res[e.to][e.from]=e.cost;
}
return res;
}
void clear(){
built=false;
inv_built=false;
edges.clear();
csr.clear();
inv_csr.clear();
start.assign(n+1,0);
inv_start.assign(n+1,0);
}
template<class F>
void sort(int i,F f){
build();
assert(0<=i&&i<n);
std::sort(csr.begin()+start[i],csr.begin()+start[i+1],f);
}
};
}
#ifdef LOCAL
#include"lib/debug/formatters/graph--base.hpp"
#endif
// library end: "lib/graph/base.hpp"
// library include: "lib/ds/union_find/uf.hpp"
// library include: "lib/graph/base.hpp"
// library begin: "lib/graph/connectivity/contraction.hpp"
// library include: "lib/template.hpp"
namespace ryoku{
template<class G>
pair<G,vc<int>>contraction(G g,vc<int>group){
map<int,int>mp;
int id=0;
for(auto&x:group){
if(!mp.count(x))mp[x]=id++;
x=mp[x];
}
G res(id);
rep(i,g.size()){
for(auto&e:g[i]){
if(G::directed()==false&&i>e.to)continue;
res.add_edge(group[i],group[e.to],e.cost);
}
}
return {res,group};
}
}
// library end: "lib/graph/connectivity/contraction.hpp"
// library include: "lib/ds/union_find/uf.hpp"
// library include: "lib/graph/base.hpp"
using namespace ryoku;
void solve(){
LL(n,m,q);
VC(ll,s,n);
static_graph<0,i128>g(n+n);
disjoint_set_union dsu(n);
disjoint_set_union dsu2(n);
vc<pii>es;
rep(i,m){
LL(u,v,w);--u,--v;
if(w==0){
dsu2.merge(u,v);
}
if(i+1<62&&(i128(1)<<(i+1))*w<=2e18){
g.add_edge(u,v,w<<(i+1));
es.pb({u,v});
}
}
vc<int>rt(n);rep(i,n)rt[i]=dsu2.root(i);
auto G=contraction(g,rt);
g=G.fi;
n=g.size();vc<ll>ns(n);rep(i,s.size())chmin(ns[G.se[i]],s[i]);
s=move(ns);
g.resize(n*2);
for(auto&[u,v]:es){
dsu.merge(G.se[u],G.se[v]);
}
vvc<int>cs(n);
rep(i,n){
cs[dsu.root(i)].pb(i);
}
rep(i,n){
for(auto&c:cs[i]){
g.add_edge(c,i+n,s[c]);
}
}
while(q--){
LL(a,b);--a,--b;
a=G.se[a],b=G.se[b];
auto A=[&](ll a){
unordered_map<ll,i128>md;
smpq<pair<i128,ll>>que;
auto push=[&](ll x,ll y){
if(!md.count(x)||md[x]>y){
md[x]=y;que.push({y,x});
}
};
push(a,0);
mark(RED);
while(que.size()){
auto[d,v]=que.top();que.pop();dbg(d,v);
if(d>2e18)break;
if(md[v]!=d)continue;
for(auto&e:g[v]){
dbg(e.to,e.cost+d);
push(e.to,e.cost+d);
}
}return md;
};
auto mda=A(a);
auto mdb=A(b);
i128 ans=4e18;
i128 mdbmin=4e18;
dbg(mda,mdb);
for(auto&[x,y]:mdb)if(x<n)chmin(mdbmin,s[x]+y);
dbg(g[1]);
for(auto&[x,y]:mda){
if(x<n)chmin(ans,mdbmin+s[x]+y);
if(x==b)chmin(ans,y);
else if(x<n){
chmin(ans,y+s[x]+s[b]);
}
}
PRT(ans);
}
}
signed main(){
int t=1;
// cin >> t;
while(t--)solve();
}
ryoku