結果

問題 No.848 なかよし旅行
ユーザー NyaanNyaanNyaanNyaan
提出日時 2019-07-05 23:22:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 7,189 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 127,124 KB
実行使用メモリ 10,708 KB
最終ジャッジ日時 2023-08-07 19:43:01
合計ジャッジ時間 3,628 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
10,708 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 25 ms
5,440 KB
testcase_12 AC 30 ms
5,632 KB
testcase_13 AC 39 ms
6,288 KB
testcase_14 AC 16 ms
4,412 KB
testcase_15 AC 34 ms
6,188 KB
testcase_16 AC 55 ms
7,856 KB
testcase_17 AC 34 ms
6,956 KB
testcase_18 AC 22 ms
5,032 KB
testcase_19 AC 22 ms
4,872 KB
testcase_20 AC 17 ms
4,380 KB
testcase_21 AC 42 ms
7,056 KB
testcase_22 AC 36 ms
7,408 KB
testcase_23 AC 22 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 57 ms
8,032 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O2")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define whlie while
#define retunr return
#define reutrn return
#define reutnr return
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define inf 1001001001
#define infLL (1LL << 61)
#define MOD 1000000007
#define FOR(i,a,b) for(int (i)=(int)(a); (i)<(int)(b); (i)++) // [a,b)
#define rep(i,N) FOR((i), 0, (int)(N)) // [0,N)
#define FORR(i,a,b) for(int (i)=(int)(b) - 1; (i)>=(int)(a); (i)--)
#define repr(i,N) FORR((i), 0, (int)(N))
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it=v.begin();it!=v.end();it++)
#define vrepr(v,it) for(auto it=v.rbegin();it!=v.rend();it++)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) double __VA_ARGS__; in(__VA_ARGS__)
#define inpii(...) pii __VA_ARGS__; in(__VA_ARGS__)
#define rand mtrand
#define randinit random_device seed_gen; mt19937 mtrand(seed_gen()+2)

#ifdef LOCAL  
  #define trc(...) do { cout << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
  #define stopif(val) assert( !(val) )
  #define trcv(v,...) do { cout << #v << " = "; vector_debug(v , ##__VA_ARGS__);cout << endl;} while(0)
#else
  #define trc(...)
  #define stopif(...)
  #define trcv(...)
#endif

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vpii = vector<pii>;
using vii = vector< vector<int> >;

struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15);} } iosetupnya;

int gcd(int a, int b){if(a>b) swap(a,b); return a==0 ? b : gcd(b%a,a);} ll gcd(ll a, ll b){if(a>b) swap(a,b); return a==0 ? b : gcd(b%a,a);}
ll lcm(int a, int b){return (1LL * a / gcd(a,b)) * b;} ll lcm(ll a, ll b){return (a / gcd(a,b)) * b;}
inline ll pow(int a, int b){ll ans = 1; rep(i,b) ans *= a; return ans;} inline ll pow(ll a, ll b){ll ans = 1; rep(i,b) ans*= a; return ans;}
inline ll pow(int a, ll b){ll ans = 1; rep(i,b) ans *= a; return ans;} inline ll pow(ll a, int b){ll ans = 1; rep(i,b) ans*= a; return ans;}
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename C> inline void _cin(C &c){cin >> c;}
template<typename T,typename U> inline void _cin(pair<T,U> &p){cin >> p.fi; cin >> p.se;}
template<typename C> inline void _cout(const C &c){cout << c;}
template<typename T,typename U> inline void _cout(const pair<T,U> &p){cout << p.fi << ' ' << p.se;}
void in(){} template <typename T,class... U> void in(T &t,U &...u){ _cin(t); in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,U ...u){ _cout(t); if(sizeof...(u)) cout << ' '; out(u...);}
template<typename C> inline void in(vector<C> &v,int N=-1){if(sz(v) != 0){int M=(N == -1) ? sz(v) : N; rep(i,M) _cin(v[i]);}else{C c;rep(i,N) v.pb((_cin(c),c));}}
template<typename C> inline void in(C v[],int N){rep(i,N) _cin(v[i]);}
template<typename C> inline void out(const vector<C> &v,int N=-1){int M=(N == -1) ? sz(v) : N; rep(i,M) {cout<<( (i)?" ":"" ); _cout(v[i]);} cout<<"\n";}
template<typename C> inline void out(C v[],int N){rep(i,N) {cout<<( (i)?" ":"" ); _cout(v[i]);} cout<<"\n";}
template<typename C> inline void vector_debug(const vector<C> &v,int N=-1){cout << "{"; int M=(N == -1) ? sz(v) : N; rep(i,M) {cout<<( (i)?", ":"" ); _cout(v[i]);} cout<<"}";}
template<typename C> inline void vector_debug(C v[], int N){cout << "{"; rep(i,N) {cout<<((i)?", ":""); _cout(*(v+i));} cout<<"}";}
void dbg_out(){cout << endl;} template <typename T,class... U> void dbg_out(const T &t,U ...u){ _cout(t); if(sizeof...(u)) cout << ", "; dbg_out(u...);}
template<typename C,class... U> void dbg_out(const vector<C> &v,U ...u){vector_debug(v); if(sizeof...(u)) cout << ", "; dbg_out(u...);}
template<typename C,class... U> void dbg_out(const vector<vector<C>> &v,U ...u){cout << "{ "; rep(i,sz(v)) {if(i)cout<<", "; vector_debug(v[i]);} cout << " }"; if(sizeof...(u)) cout << ", "; dbg_out(u...);}
template<typename C> inline C vmax(const vector<C> &v){C n=v[0]; rep(i,sz(v)) amax(n,v[i]); return n;} template<typename C> inline C vmax(C v[], int N){C n=v[0]; rep( i , N ) amax(n,v[i]); return n;}
template<typename C> inline C vmin(const vector<C> &v){C n=v[0]; rep(i,sz(v)) amin(n,v[i]); return n;} template<typename C> inline C vmin(C v[], int N){C n=v[0]; rep( i , N ) amin(n,v[i]); return n;}

////////////
/// main ///
////////////
template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) { // edge e = n; とできる(e.to = n)
    to = x;
    return *this;
  }

  operator int() const { return to; } // v[e]とできる ( v[e.to]に同じ )

};

template<typename T>
bool operator <(const edge<T>& e1,const edge<T>& e2){ // sort用 costの小さいほうが左
  return e1.cost < e2.cost;
}
template< typename T >
using Edges = vector< edge< T > >;
template<typename T>
using WG = vector< Edges< T > >;

// ダイクストラ法 計算量 O( log(頂点数N) * 辺の数E)
// s...始点 V...頂点の数 D...最短距離を記録するvector G...重み付きグラフ
template<typename T>
void dkstr(int s, int V, vector<T>& D, WG<T> &G){ 
    typedef pair<T,int> P; // pair(最短距離,頂点の番号)
    int nodes = V;
    rep(i,nodes) D[i] = infLL; // long longの場合 infLL にする!!!
    priority_queue<P,vector<P>,greater<P>> Q;
    D[s] = 0;
    Q.push(mp(0,s));
    whlie(!Q.empty()){
        P p = Q.top(); Q.pop();
        int v = p.se;
        if(D[v] < p.fi) continue;
        for(auto &e : G[v]){ // G 重み付きグラフ
            if( amin(D[e], D[v] + e.cost) ){
                Q.push(mp(D[e], e));
            }
        }
    }
}

WG<ll> g;
vector<ll> p,q,s;

int main(){
  inl(N,M,P,Q,T); P--; Q--;
  g.resize(N);
  p.resize(N); q.resize(N); s.resize(N);
  rep(hoge,M){
    inl(a,b,c); a--; b--;
    g[a].eb(b,c);
    g[b].eb(a,c);
  }

  dkstr<ll>(0,N,s,g);
  dkstr<ll>(P,N,p,g);
  dkstr<ll>(Q,N,q,g);

  if(s[P] + p[Q] + s[Q] <= T){
    out(T); return 0;
  }
  ll ans = -1;
  rep(i,N) rep(j,N){
    trc(i,j);
    if(s[i] + p[i] + p[Q] + q[j] + s[j] <= T){
      out(T); return 0;
    }
    if(s[i] + s[j] + max(p[i] + p[j], q[i] + q[j]) > T) continue;
    amax(ans , T - max(p[i] + p[j], q[i] + q[j]) );
  }
  out(ans);



}
0