結果
| 問題 | No.1320 Two Type Min Cost Cycle |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-01-18 19:10:33 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,087 ms / 2,000 ms |
| コード長 | 8,447 bytes |
| 記録 | |
| コンパイル時間 | 4,948 ms |
| コンパイル使用メモリ | 363,056 KB |
| 実行使用メモリ | 7,852 KB |
| 最終ジャッジ日時 | 2026-01-18 19:10:48 |
| 合計ジャッジ時間 | 15,057 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 57 |
ソースコード
#include<bits/stdc++.h>
#if __has_include(<atcoder/all>)
#endif
using namespace std;
#define eb emplace_back
#define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define RDVV(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);vin(__VA_ARGS__)
#define VV(n,...) RDVV(ll,n,__VA_ARGS__)
#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)
#define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);}
#define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{
namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
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 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 is_negative=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-is_negative*2);}
template<class T>constexpr bool operator==(const T&x)const{return static_cast<T>(*this)==x;}
};
constexpr infinity oo;
void dec(auto&...a){((--a),...);}
decltype(auto)rev(auto&&a){ranges::reverse(a);return a;}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace my{
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 my{
template<class T>struct queue:std::queue<T>{
queue()=default;
queue(const initializer_list<T>&a){fe(a,e)this->emplace(e);}
ll size()const{return std::queue<T>::size();}
T pop(){T r=this->front();std::queue<T>::pop();return r;}
};
template<class T,class F>struct priority_queue:std::priority_queue<T,vector<T>,F>{
priority_queue()=default;
priority_queue(const initializer_list<T>&a){fe(a,e)this->emplace(e);}
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 my{
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){}
auto operator<=>(const edge&e)const{return wt<=>e.wt;}
};
}
namespace my{
template<class WT>class RootedTree{
public:
ll n_;
vec<vec<edge<WT>>>edges_;
vec<edge<WT>>rev_edge_;
int root_;
RootedTree()=default;
RootedTree(ll n,ll r):n_(n),edges_(n),rev_edge_(n,{-1,-1,WT{},-1}),root_(r){}
ll size()const{return n_;}
auto par(ll u)const{return u==root_?-1:rev_edge_[u].from;}
auto root_child_enumerate()const{
vec<ll>res(size(),-1);
fe(bfs_order(root_),u){
if(u==root_)continue;
if(par(u)==root_)res[u]=u;
fe(edges_[u],e)res[e.to]=res[u];
}
return res;
}
void set_edge(const edge<WT>&e){
edges_[e.from].eb(e);
rev_edge_[e.to]=e;
}
auto dep_enumerate(int start,int stop=-1)const{
vec<ll>res(size(),-1);
queue<pair<int,int>>q{{start,stop}};
res[start]=0;
while(q.size()){
auto[u,pre]=q.pop();
if(u==stop)continue;
if(u!=root_){
auto p=par(u);
if(p!=pre){
res[p]=res[u]+1;
q.emplace(p,u);
}
}
fe(edges_[u],e){
if(e.to==pre)continue;
res[e.to]=res[u]+1;
q.emplace(e.to,u);
}
}
return res;
}
auto bfs_order(ll start=-1)const{
if(start==-1)start=root_;
vec<ll>ord;
queue<ll>q{start};
while(q.size()){
ll u=q.pop();
ord.eb(u);
fe(edges_[u],e)q.emplace(e.to);
}
return ord;
}
};
}
namespace my{
template<class WT>class Graph{
public:
ll n_;
vec<vec<edge<WT>>>edges_;
Graph()=default;
Graph(ll n):n_(n),edges_(n){}
decltype(auto)operator[](ll i)const{return edges_[i];}
ll size()const{return edges_.size();}
auto&add_edges(const vec<ll>&a,const vec<ll>&b,const vec<WT>&w){fo(i,a.size())edges_[a[i]].eb(a[i],b[i],w[i],i);return*this;}
auto dijkstra_tree(int s)const{
RootedTree<WT>tree(n_,s);
vec<WT>dist(n_,oo);
dist[s]=WT{};
vec<bool>used(n_);
min_heap<pair<WT,edge<WT>>>q{{WT{},{-1,s,WT{},-1}}};
while(q.size()){
auto[du,pu]=q.pop();
auto u=pu.to;
if(dist[u]<du)continue;
if(used[u])continue;
used[u]=1;
if(pu.from!=-1)tree.set_edge(pu);
fe(edges_[u],e){
if(amin(dist[e.to],dist[u]+e.wt)){
assert(e.wt>=WT{});
assert(!used[e.to]);
q.emplace(dist[e.to],e);
}
}
}
return tree;
}
auto dijkstra_dist_enumerate(int s)const{
vec<WT>dist(n_,oo);
dist[s]=WT{};
vec<bool>used(n_);
min_heap<pair<WT,edge<WT>>>q{{WT{},{-1,s,WT{},-1}}};
while(q.size()){
auto[du,pu]=q.pop();
auto u=pu.to;
if(dist[u]<du)continue;
if(used[u])continue;
used[u]=1;
fe(edges_[u],e){
if(amin(dist[e.to],dist[u]+e.wt)){
assert(e.wt>=WT{});
assert(!used[e.to]);
q.emplace(dist[e.to],e);
}
}
}
return dist;
}
};
}
namespace my{
template<class WT>auto undirected_shortest_cycle(const Graph<WT>&g,ll s){
auto dist=g.dijkstra_dist_enumerate(s);
auto spt=g.dijkstra_tree(s);
WT cost=oo;
ll a=-1,b=-1;
vec mi(g.size(),WT{oo});
fe(g[s],e){
if(e.to==s){
if(amin(cost,e.wt))a=b=s;
continue;
}
if(mi[e.to]==oo)mi[e.to]=e.wt;
else if(amin(cost,mi[e.to]+e.wt))a=s,b=e.to;
}
auto dep=spt.dep_enumerate(s);
fe(g[s],e){
if(dep[e.to]<=1||dep[e.to]==oo)continue;
if(amin(cost,dist[e.to]+e.wt))a=s,b=e.to;
}
auto roch=spt.root_child_enumerate();
fo(u,g.size()){
if(u==s)continue;
if(dist[u]==oo)continue;
fe(g[u],e){
if(e.to==s)continue;
if(dist[e.to]==oo)continue;
if(roch[u]==roch[e.to])continue;
if(amin(cost,dist[u]+dist[e.to]+e.wt))a=u,b=e.to;
}
}
vec<ll>cycle;
while(a!=-1&&a!=s)cycle.eb(a),a=spt.par(a);
cycle.eb(s);
rev(cycle);
while(b!=-1&&b!=s)cycle.eb(b),b=spt.par(b);
return pair{cycle,cost};
}
template<class WT>auto directed_shortest_cycle(const Graph<WT>&g,ll s){
auto dist=g.dijkstra_dist_enumerate(s);
auto spt=g.dijkstra_tree(s);
WT cost=oo;
ll a=-1;
fo(u,g.size())fe(g[u],e)if(e.to==s&&amin(cost,dist[u]+e.wt))a=u;
vec<ll>cycle;
while(a!=-1&&a!=s)cycle.eb(a),a=spt.par(a);
cycle.eb(s);
return pair{rev(cycle),cost};
}
}
namespace my{entry
void main(){
LL(T,N,M);
VV(M,a,b,c);dec(a,b);
Graph<ll>g(N);
ll ans=oo;
if(T==0){
g.add_edges(a,b,c);
g.add_edges(b,a,c);
fo(i,N)amin(ans,undirected_shortest_cycle(g,i).b);
}else{
g.add_edges(a,b,c);
fo(i,N)amin(ans,directed_shortest_cycle(g,i).b);
}
pp(ans!=oo?ans:-1);
}}