結果
| 問題 | No.3810 Binary Shortest Path Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-20 23:26:28 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
WA
不安定
|
| 実行時間 | - |
| コード長 | 8,933 bytes |
| 記録 | |
| コンパイル時間 | 3,627 ms |
| コンパイル使用メモリ | 370,704 KB |
| 実行使用メモリ | 29,112 KB |
| 最終ジャッジ日時 | 2026-11-20 23:26:40 |
| 合計ジャッジ時間 | 10,900 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge5_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 WA * 22 |
ソースコード
/*
#include<kyopro/master>
void self::main(){
LL(N,M,Q);
VL(N,a);
VV(M,u,v,w);dec(u,v);
DsuPlain uf(N);
fo(i,M){
if(w[i]==0)uf.merge(u[i],v[i]);
}
auto group=uf.groups();
ll C=group.size();
vec<ll>gid(N);
fo(k,C)fe(group[k],i)gid[i]=k;
Graph<ll>g(C);
fo(i,M){
if(uf.same(u[i],v[i]))continue;
ll cost=(OverflowInt<ll>(w[i])*OverflowInt<ll>(2).pow(i+1)).val();
if(cost==oo)continue;
g.add_edge(gid[u[i]],gid[v[i]],cost);
g.add_edge(gid[v[i]],gid[u[i]],cost);
}
vec<ll>warp(C,oo);
fo(i,N)amin(warp[gid[i]],a[i]);
FO(Q){
LL(s,t);dec(s,t);
unordered_map<ll,ll>dp;
dp[gid[s]]=0;
min_heap<pair<ll,ll>>q;
q.emplace(0,gid[s]);
while(q.size()){
auto[du,u]=q.pop();
if(dp[u]<du)continue;
fe(g[u],e){
if(!dp.contains(e.to)){
dp[e.to]=du+e.wt;
q.emplace(dp[e.to],e.to);
}else if(amin(dp[e.to],du+e.wt)){
q.emplace(dp[e.to],e.to);
}
}
}
unordered_map<ll,ll>ep;
ep[gid[t]]=0;
min_heap<pair<ll,ll>>{}.swap(q);
q.emplace(0,gid[t]);
while(q.size()){
auto[du,u]=q.pop();
if(ep[u]<du)continue;
fe(g[u],e){
if(!ep.contains(e.to)){
ep[e.to]=du+e.wt;
q.emplace(ep[e.to],e.to);
}else if(amin(ep[e.to],du+e.wt)){
q.emplace(ep[e.to],e.to);
}
}
}
ll res=oo;
if(dp.contains(gid[t]))amin(res,dp[gid[t]]);
// s-x...y-t : -:辺移動, ...:ワープ
fe(dp,x,c)fe(ep,y,d)amin(res,c+(x!=y?warp[x]+warp[y]:0)+d);
pp(res);
}
}
*/
#include<bits/stdc++.h>
using namespace std;
namespace self{void main();void sub();void pp(auto&&...a);void ppe(auto&&...a);}
int main(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);self::main();}
#define eb emplace_back
#define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define RDVL(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);lin(__VA_ARGS__)
#define RDVV(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);vin(__VA_ARGS__)
#define VL(n,...) RDVL(ll,n,__VA_ARGS__)
#define VV(n,...) RDVV(ll,n,__VA_ARGS__)
#define FO(n) for(ll IJK=n;IJK-->0;)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
namespace self{
using ll=long long;
using lll=__int128_t;
constexpr auto refs(auto&...a){return array{ref(a)...};}
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
constexpr auto pow(auto x,ll n,auto e){assert(n>=0);decltype(x)r=e;for(;n;x*=x,n>>=1)if(n&1)r*=x;return r;}
constexpr auto pow(auto x,ll n){return pow(x,n,1);}
constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;}
template<class T>constexpr ll maxsize10(){return size10(numeric_limits<T>::max());}
bool amin(auto&a,const auto&b){return b<a?a=b,1:0;}
template<class A,class B=A>struct pair{
A a;B b;
pair()=default;
pair(A a,B b):a(a),b(b){}
auto operator<=>(const pair&)const=default;
};
template<bool neg=false>struct infinity{
template<integral T>static constexpr T ones(size_t n){return n?ones<T>(n-1)*10+1:0;}
template<integral T>constexpr operator T()const{return ones<T>(maxsize10<T>())*(1-neg*2);}
template<class T>constexpr bool operator==(const T&x)const{return static_cast<T>(*this)==x;}
constexpr auto operator-()const{return infinity<!neg>();}
};
constexpr infinity oo;
void dec(auto&...a){((--a),...);}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace self{
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>constexpr int depth=0;
template<class T>struct core_t_helper{using type=T;};
template<class T>using core_t=core_t_helper<T>::type;
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
static constexpr int D=depth<V>+1;
using C=core_t<V>;
using vector<V>::vector;
void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else{ }}
static auto make(ll n,const auto&...a){
if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
else { }
}
vec&operator--(){fe(*this,e)--e;return*this;}
ll size()const{return vector<V>::size();}
auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
}
namespace self {
template<integral T>class OverflowInt{
public:
using integer=OverflowInt;
T a_=0;
int inf_=0;
OverflowInt()=default;
OverflowInt(auto x):a_(x){}
int sign()const{return inf_?inf_:(a_>0)-(a_<0);}
T val()const{
if(inf_>0)return oo;
if(inf_<0)return-oo;
return a_;
}
integer&operator*=(const integer&b){
if(inf_||b.inf_){
if(sign()==0||b.sign()==0)throw domain_error("0*oo");
inf_=sign()*b.sign();
return*this;
}
T x;
if(__builtin_mul_overflow(a_,b.a_,&x))inf_=sign()*b.sign();
else a_=x;
return*this;
}
friend integer operator*(integer a,const integer&b){return a*=b;}
integer pow(lll n)const{assert(n>=0);return self::pow(*this,n);}
};
}
namespace self{
class DsuPlain{
public:
ll n_;
vec<int>par_or_size_;
DsuPlain(ll n):n_(n),par_or_size_(n,-1){}
auto merge(ll a,ll b){
ll x=leader(a),y=leader(b);
if(x==y)return x;
if(-par_or_size_[x]<-par_or_size_[y])swap(x,y),swap(a,b);
par_or_size_[x]+=par_or_size_[y];
par_or_size_[y]=x;
return x;
}
bool same(ll a,ll b){return leader(a)==leader(b);}
ll leader(ll a){
if(par_or_size_[a]<0)return a;
return par_or_size_[a]=leader(par_or_size_[a]);
}
auto groups(){
vec<int>leader_buf(n_),group_size(n_);
fo(u,n_){
leader_buf[u]=leader(u);
group_size[leader_buf[u]]++;
}
vec<vec<ll>>res(n_);
fo(u,n_)res[u].reserve(group_size[u]);
fo(u,n_)res[leader_buf[u]].eb(u);
res.erase(remove_if(res.begin(),res.end(),[&](const vec<ll>&v){return v.empty();}),res.end());
return res;
}
};
}
namespace self{
template<class T,class F>struct priority_queue:std::priority_queue<T,vector<T>,F>{
priority_queue()=default;
ll size()const{return std::priority_queue<T,vector<T>,F>::size();}
T pop(){T r=this->top();std::priority_queue<T,vector<T>,F>::pop();return r;}
};
template<class T>using min_heap=priority_queue<T,greater<T>>;
}
namespace self{
template<class WT>struct edge{
int from,to;
WT wt;
int id;
edge()=default;
edge(int from,int to,WT wt=1,int id=-1):from(from),to(to),wt(wt),id(id){}
};
}
namespace self{
template<class WT>class Graph{
private:
public:
ll n_;
vec<vec<edge<WT>>>edges_;
Graph()=default;
Graph(ll n):n_(n),edges_(n){}
decltype(auto)operator[](ll i){return edges_[i];}
void add_edge(ll a,auto&&...b){edges_[a].eb(a,std::forward<decltype(b)>(b)...);}
};
}
void self::pp(auto&&...a){const char*c="";((cout<<c<<a,c=" "),...);cout<<'\n';}
void self::main(){
LL(N,M,Q);
VL(N,a);
VV(M,u,v,w);dec(u,v);
DsuPlain uf(N);
fo(i,M){
if(w[i]==0)uf.merge(u[i],v[i]);
}
auto group=uf.groups();
ll C=group.size();
vec<ll>gid(N);
fo(k,C)fe(group[k],i)gid[i]=k;
Graph<ll>g(C);
fo(i,M){
if(uf.same(u[i],v[i]))continue;
ll cost=(OverflowInt<ll>(w[i])*OverflowInt<ll>(2).pow(i+1)).val();
if(cost==oo)continue;
g.add_edge(gid[u[i]],gid[v[i]],cost);
g.add_edge(gid[v[i]],gid[u[i]],cost);
}
vec<ll>warp(C,oo);
fo(i,N)amin(warp[gid[i]],a[i]);
FO(Q){
LL(s,t);dec(s,t);
unordered_map<ll,ll>dp;
dp[gid[s]]=0;
min_heap<pair<ll,ll>>q;
q.emplace(0,gid[s]);
while(q.size()){
auto[du,u]=q.pop();
if(dp[u]<du)continue;
fe(g[u],e){
if(!dp.contains(e.to)){
dp[e.to]=du+e.wt;
q.emplace(dp[e.to],e.to);
}else if(amin(dp[e.to],du+e.wt)){
q.emplace(dp[e.to],e.to);
}
}
}
unordered_map<ll,ll>ep;
ep[gid[t]]=0;
min_heap<pair<ll,ll>>{}.swap(q);
q.emplace(0,gid[t]);
while(q.size()){
auto[du,u]=q.pop();
if(ep[u]<du)continue;
fe(g[u],e){
if(!ep.contains(e.to)){
ep[e.to]=du+e.wt;
q.emplace(ep[e.to],e.to);
}else if(amin(ep[e.to],du+e.wt)){
q.emplace(ep[e.to],e.to);
}
}
}
ll res=oo;
if(dp.contains(gid[t]))amin(res,dp[gid[t]]);
fe(dp,x,c)fe(ep,y,d)amin(res,c+(x!=y?warp[x]+warp[y]:0)+d);
pp(res);
}
}