結果
問題 | No.1069 電柱 / Pole (Hard) |
ユーザー |
![]() |
提出日時 | 2020-04-26 18:40:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 646 ms / 2,000 ms |
コード長 | 3,860 bytes |
コンパイル時間 | 3,341 ms |
コンパイル使用メモリ | 221,164 KB |
最終ジャッジ日時 | 2025-01-10 02:03:18 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 79 |
ソースコード
#include<bits/stdc++.h> #define ALL(v) std::begin(v),std::end(v) using lint=long long; using ld=long double; template<class T>using numr=std::numeric_limits<T>; struct input_t{template<class T>operator T(){T t;std::cin>>t;return t;}}input; using path_t=std::vector<std::pair<lint,ld>>; struct compare_t{ bool operator()(path_t const& a, path_t const& b)const{return a.back().second>b.back().second;}; }; template<std::size_t N,class T,class...Args>auto mkvec(std::size_t sz,Args...args){ if constexpr(N==1)return std::vector<T>(sz,args...); else return std::vector(sz,mkvec<N-1,T>(args...)); } int main(){ std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false); std::cout.setf(std::ios_base::fixed);std::cout.precision(15); lint n=input,m=input,K=input; lint s=input,t=input;s--,t--; std::vector<std::pair<ld,ld>>pt(n); for(auto&p:pt)std::cin>>p.first>>p.second; std::vector<std::vector<std::pair<lint,ld>>>g(n); while(m--){ lint u=input,v=input;u--,v--; ld w=std::hypot(pt.at(u).first-pt.at(v).first,pt.at(u).second-pt.at(v).second); g.at(u).emplace_back(v,w); g.at(v).emplace_back(u,w); } ld inf=numr<ld>::infinity(); ld eps=1e-7; std::vector<path_t>ans; auto dijkstra=[&](lint start, auto&&dead)->path_t{ std::priority_queue<std::pair<ld,lint>>que; std::vector<ld>dist(n,inf),prev(n); que.emplace(0,start); dist.at(start)=0; while(!que.empty()){ lint x=que.top().second;que.pop(); for(auto[y,w]:g.at(x)){ ld nd=dist.at(x)+w; if(dead.at(y)==2||dead.at(y)==1&&x==start||dist.at(y)-eps<=nd)continue; dist.at(y)=nd; prev.at(y)=x; que.emplace(-nd,y); } } if(dist.at(t)==inf)return {}; path_t path={{t,dist.at(t)}}; while(path.back().first!=start){ lint y=prev.at(path.back().first); path.emplace_back(y,dist.at(y)); } return path; }; { auto path=dijkstra(s,std::vector<lint>(n,0)); std::reverse(ALL(path)); ans.push_back(std::move(path)); } auto coprefix=[](auto&&a,auto&&b,std::size_t l){ if(a.size()<l||b.size()<l)return false; for(std::size_t i=0;i<l;i++){ if(a.at(i).first!=b.at(i).first)return false; } return true; }; std::priority_queue<path_t,std::vector<path_t>,compare_t>que; for(lint k=1;k<K;k++){ auto const& b0=ans.back(); lint l0=b0.size(); for(lint j=1;j<l0;j++){ std::vector<lint>dead(n); for(auto&&b:ans)if(coprefix(b,b0,j))dead.at(b.at(j).first)=1; for(lint j_=0;j_<j;j_++)dead.at(b0.at(j_).first)=2; auto path=dijkstra(b0.at(j-1).first,dead); if(path.empty())continue; for(auto&&p:path)p.second+=b0.at(j-1).second; for(lint j_=j-2;j_>=0;j_--)path.push_back(b0.at(j_)); std::reverse(ALL(path)); que.push(path); } bool found=false; while(!que.empty()){ auto path=que.top();que.pop(); lint sz=path.size(); bool ng=false; for(auto&&b:ans){ if((lint)b.size()!=sz)continue; for(lint i=0;i<=sz;i++){ if(i==sz){ ng=true; break; } if(path.at(i).first!=b.at(i).first)break; } } if(!ng){ found=true; ans.push_back(path); break; } } if(!found)break; } for(lint i=0;i<K;i++){ std::cout<<(i<(lint)ans.size()?ans.at(i).back().second:-1)<<'\n'; } }