結果

問題 No.3013 ハチマキ買い星人
ユーザー Taiki0715
提出日時 2025-08-30 20:16:04
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 10,045 bytes
コンパイル時間 3,926 ms
コンパイル使用メモリ 295,256 KB
実行使用メモリ 29,028 KB
最終ジャッジ日時 2025-08-30 20:16:18
合計ジャッジ時間 13,060 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T1,typename T2,typename T3>istream &operator>>(istream &is,tuple<T1,T2,T3>&a){is>>std::get<0>(a)>>std::get<1>(a)>>std::get<2>(a);return is;}
template<typename T,size_t n>istream &operator>>(istream &is,array<T,n>&a){for(auto&i:a)is>>i;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define overload3(_1,_2,_3,name,...) name
#define rep1(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define reps(i,l,r) rep2(i,l,r)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<(x)<<'\n',static_cast<void>(0)
#define yn(x) cout<<((x)?"Yes\n":"No\n")
#define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end())
template<typename T>
inline int fkey(vector<T>&z,T key){return lower_bound(z.begin(),z.end(),key)-z.begin();}
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
#ifdef LOCAL
#include<debug.h>
#define SWITCH(a,b) (a)
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define SWITCH(a,b) (b)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
struct Timer{
  clock_t start;
  Timer(){
    start=clock();
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout<<fixed<<setprecision(16);
  }
  inline double now(){return (double)(clock()-start)/1000;}
  #ifdef LOCAL
  ~Timer(){
    cerr<<"time:";
    cerr<<now();
    cerr<<"ms\n";
  }
  #endif
}timer;
void SOLVE();
int main(){
  int testcase=1;
  //cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
}
template<typename T=int>
struct Edge{
  int from,to;
  T weight;
  int index;
  Edge(int from_,int to_,T weight_=T(),int index_=-1):from(from_),to(to_),weight(weight_),index(index_){}
  Edge():from(-1),to(-1),weight(),index(-1){}
  friend std::ostream &operator<<(std::ostream &os,const Edge&e){
    os<<'[';
    os<<"from:"<<e.from;
    os<<"to:"<<e.to;
    os<<"weight:"<<e.weight;
    os<<"index:"<<e.index;
    os<<']';
    return os;
  }
};
template<typename T=int>
struct Graph{
private:
  int n;
  std::vector<Edge<T>>edge;
  std::vector<Edge<T>>g;
  std::vector<int>ptr;
  bool directed;
  struct graph_range{
    using iterator=typename std::vector<Edge<T>>::iterator;
    iterator l,r;
    iterator begin()const{return l;}
    iterator end()const{return r;}
    int size()const{return r-l;}
    Edge<T> &operator[](int i)const{return l[i];}
  };
  struct const_graph_range{
    using iterator=typename std::vector<Edge<T>>::const_iterator;
    iterator l,r;
    iterator begin()const{return l;}
    iterator end()const{return r;}
    int size()const{return r-l;}
    const Edge<T> &operator[](int i)const{return l[i];}
  };
public:
  Graph(int n_,bool dir_):n(n_),directed(dir_){}
  Graph():n(0){}
  Graph(int n_,bool dir_,const std::vector<Edge<T>>&e):n(n_),directed(dir_),edge(e){build();}
  template<bool weighted=false,bool index=1>
  void read(int m){
    edge.reserve(m);
    for(int i=0;i<m;i++){
      int u,v;
      std::cin>>u>>v;
      T w;
      if constexpr(index)u--,v--;
      if constexpr(weighted)std::cin>>w;
      else w=1;
      edge.emplace_back(u,v,w,i);
    }
    build();
  }
  void add_edge(int u,int v){
    int id=edge.size();
    edge.emplace_back(u,v,1,id);
  }
  void add_edge(int u,int v,T w){
    int id=edge.size();
    edge.emplace_back(u,v,w,id);
  }
  void add_edge(int u,int v,T w,int index){
    edge.emplace_back(u,v,w,index);
  }
  void build(){
    std::vector<int>cnt(n+1,0);
    for(const Edge<T>&e:edge){
      cnt[e.from+1]++;
      if(!directed)cnt[e.to+1]++;
    }
    for(int i=1;i<=n;i++)cnt[i]+=cnt[i-1];
    ptr=cnt;
    g.resize(cnt[n]);
    for(const Edge<T>&e:edge){
      g[cnt[e.from]++]=e;
      if(!directed)g[cnt[e.to]++]=Edge<T>(e.to,e.from,e.weight,e.index);
    }
  }
  void reverse(){
    if(directed){
      for(Edge<T>&e:edge)std::swap(e.from,e.to);
      build();
    }
  }
  inline void to_directed(){
    directed=true;
    build();
  }
  inline void to_undirected(){
    directed=false;
    build();
  }
  void reserve(int m){edge.reserve(m);}
  graph_range operator[](int i){return graph_range{g.begin()+ptr[i],g.begin()+ptr[i+1]};}
  const_graph_range operator[](int i)const{return const_graph_range{g.begin()+ptr[i],g.begin()+ptr[i+1]};}
  const Edge<T>& get_edge(int i)const{return edge[i];}
  std::vector<Edge<T>>get_edges()const{return edge;}
  inline bool is_directed()const{return directed;}
  inline int size()const{return n;}
  inline int edge_size()const{return edge.size();}
  typename std::vector<Edge<T>>::iterator begin(){return edge.begin();}
  typename std::vector<Edge<T>>::iterator end(){return edge.end();}
  typename std::vector<Edge<T>>::const_iterator begin()const{return edge.begin();}
  typename std::vector<Edge<T>>::const_iterator end()const{return edge.end();}
};
#include<type_traits>
#include<concepts>
template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);}

template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>floor_pow2(T n){return n==0?0:T(1)<<msb(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);}

template<std::integral T>
constexpr T safe_div(T a,T b){return a/b-(a%b&&(a^b)<0);}
template<std::integral T>
constexpr T safe_ceil(T a,T b){return a/b+(a%b&&(a^b)>0);}
using namespace std;
template<typename T>
struct radix_heap{
  static_assert(is_integral_v<T>);
private:
  int sz;
  T last;
  vector<pair<T,int>>h[1+numeric_limits<T>::digits];
  vector<pair<int,int>>p;
public:
  radix_heap(int n):p(n,make_pair(0,-1)),sz(0),last(0){}
  inline bool empty()const{return sz==0;}
  int pop(){
    if(h[0].empty()){
      int b=0;
      while(h[b].empty())b++;
      last=h[b].back().first;
      for(int j=0;j<(int)h[b].size();j++)if(last>h[b][j].first)last=h[b][j].first;
      for(int j=0;j<(int)h[b].size();j++){
        int i=h[b][j].second;
        int nb=msb(h[b][j].first^last)+1;
        p[i]=make_pair(nb,(int)h[nb].size());
        h[nb].emplace_back(move(h[b][j]));
      }
      h[b].clear();
    }
    --sz;
    int i=h[0].back().second;
    p[i].second=-1;
    h[0].pop_back();
    return i;
  }
  void decrease_key(int i,T v){
    if(p[i].second<0){
      int b=msb(v^last)+1;
      ++sz;
      p[i]=make_pair(b,(int)h[b].size());
      h[b].emplace_back(v,i);
    }
    else if(h[p[i].first][p[i].second].first>v){
      int preb=p[i].first,nxtb=msb(v^last)+1;
      if(nxtb<preb){
        int i2=h[preb].back().second,j=p[i].second;
        swap(h[preb][j],h[preb].back());
        p[i2].second=j;
        p[i]=make_pair(nxtb,(int)h[nxtb].size());
        h[nxtb].emplace_back(v,i);
        h[preb].pop_back();
      }
      else h[preb][p[i].second].first=v;
    }
  }
};
template<typename T,std::enable_if_t<std::is_integral_v<T>,std::nullptr_t> =nullptr>
std::pair<std::vector<T>,std::vector<int>>dijkstra(const Graph<T>&g,int s=0){
  int n=g.size();
  std::vector<T>dst(n,std::numeric_limits<T>::max());
  std::vector<int>pre(n,-1);
  dst[s]=0;
  radix_heap<T>heap(n);
  heap.decrease_key(s,0);
  while(!heap.empty()){
    int x=heap.pop();
    for(const Edge<T>&e:g[x]){
      if(dst[e.to]>dst[e.from]+e.weight){
        pre[e.to]=e.from;
        dst[e.to]=dst[e.from]+e.weight;
        heap.decrease_key(e.to,dst[e.to]);
      }
    }
  }
  return std::make_pair(dst,pre);
}
template<typename T,std::enable_if_t<std::is_floating_point_v<T>,std::nullptr_t> =nullptr>
std::pair<std::vector<T>,std::vector<int>>dijkstra(const Graph<T>&g,int s=0){
  int n=g.size();
  std::vector<T>dst(n,std::numeric_limits<T>::max());
  std::vector<int>pre(n,-1);
  std::priority_queue<std::pair<T,int>,std::vector<std::pair<T,int>>,std::greater<std::pair<T,int>>>que;
  dst[s]=0;
  que.push({std::make_pair(0,s)});
  while(!que.empty()){
    auto [d,x]=que.top();
    que.pop();
    if(dst[x]!=d)continue;
    for(const Edge<T>&e:g[x]){
      T nxt=d+e.weight;
      if(dst[e.to]>nxt){
        pre[e.to]=x;
        dst[e.to]=nxt;
        que.push(std::make_pair(dst[e.to],e.to));
      }
    }
  }
  return std::make_pair(dst,pre);
}
void SOLVE(){
  int n,m,p;
  ll y;
  cin>>n>>m>>p>>y;
  Graph<ll>g(n,false);
  g.read<1>(m);
  vector<ll>store(n,-1);
  rep(i,p){
    int d,e;
    cin>>d>>e;
    d--;
    store[d]=e;
  }
  auto dst=dijkstra(g).first;
  ll ans=0;
  rep(i,n)if(dst[i]<=y&&store[i]!=-1){
    chmax(ans,(y-dst[i])/store[i]);
  }
  cout<<ans<<endl;

}
0