// s からの最短距離が定まるなら最短距離, 無限に小さく出来るなら std::nullopt// そもそも到達出来ない場合は pre が -1 になっているtemplate<typenameWG,typenameT=typenameWG::weight_type>std::pair<std::vector<std::optional<T>>,std::vector<int>>bellman_ford(constWG&g,ints=0){assert(g.is_prepared());intn=g.n;staticconstexprTINF=std::numeric_limits<T>::max()/2;std::vector<T>d(n,INF);std::vector<int>pre(n,-1);d[s]=0;for(int_=0;_<n;_++){boolupdate=false;for(intv=0;v<n;v++)if(d[v]<INF)for(constauto&e:g[v])if(d[e.to]>d[v]+e.weight){d[e.to]=d[v]+e.weight;pre[e.to]=v;update=true;}if(!update){std::vector<std::optional<T>>d_(n);for(inti=0;i<n;i++)d_[i]=d[i];return{d_,pre};}}autonow_d=d;for(intv=0;v<n;v++)if(d[v]<INF)for(constauto&e:g[v])if(d[e.to]>d[v]+e.weight)d[e.to]=d[v]+e.weight;for(int_=1;_<n;_++)for(intv=0;v<n;v++)if(d[v]<now_d[v])for(constauto&e:g[v])if(d[e.to]>d[v]+e.weight)d[e.to]=d[v]+e.weight;std::vector<std::optional<T>>res(n);for(intv=0;v<n;v++)if(now_d[v]==d[v])res[v]=d[v];elseres[v]=std::nullopt;return{res,pre};}
#line 1 "library/graph/shortest_path/BellmanFord.hpp"
// s からの最短距離が定まるなら最短距離, 無限に小さく出来るなら std::nullopt// そもそも到達出来ない場合は pre が -1 になっているtemplate<typenameWG,typenameT=typenameWG::weight_type>std::pair<std::vector<std::optional<T>>,std::vector<int>>bellman_ford(constWG&g,ints=0){assert(g.is_prepared());intn=g.n;staticconstexprTINF=std::numeric_limits<T>::max()/2;std::vector<T>d(n,INF);std::vector<int>pre(n,-1);d[s]=0;for(int_=0;_<n;_++){boolupdate=false;for(intv=0;v<n;v++)if(d[v]<INF)for(constauto&e:g[v])if(d[e.to]>d[v]+e.weight){d[e.to]=d[v]+e.weight;pre[e.to]=v;update=true;}if(!update){std::vector<std::optional<T>>d_(n);for(inti=0;i<n;i++)d_[i]=d[i];return{d_,pre};}}autonow_d=d;for(intv=0;v<n;v++)if(d[v]<INF)for(constauto&e:g[v])if(d[e.to]>d[v]+e.weight)d[e.to]=d[v]+e.weight;for(int_=1;_<n;_++)for(intv=0;v<n;v++)if(d[v]<now_d[v])for(constauto&e:g[v])if(d[e.to]>d[v]+e.weight)d[e.to]=d[v]+e.weight;std::vector<std::optional<T>>res(n);for(intv=0;v<n;v++)if(now_d[v]==d[v])res[v]=d[v];elseres[v]=std::nullopt;return{res,pre};}