結果

問題 No.848 なかよし旅行
ユーザー umezoumezo
提出日時 2024-01-23 06:28:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,434 bytes
コンパイル時間 2,615 ms
コンパイル使用メモリ 214,216 KB
実行使用メモリ 10,748 KB
最終ジャッジ日時 2024-01-23 06:28:49
合計ジャッジ時間 4,544 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
10,748 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 21 ms
6,676 KB
testcase_12 AC 27 ms
6,676 KB
testcase_13 AC 34 ms
6,676 KB
testcase_14 AC 17 ms
6,676 KB
testcase_15 AC 33 ms
6,676 KB
testcase_16 AC 51 ms
8,064 KB
testcase_17 AC 37 ms
7,040 KB
testcase_18 AC 21 ms
6,676 KB
testcase_19 AC 20 ms
6,676 KB
testcase_20 AC 14 ms
6,676 KB
testcase_21 AC 43 ms
7,168 KB
testcase_22 AC 38 ms
7,424 KB
testcase_23 AC 13 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 53 ms
8,064 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

Graph G;
vector<ll> dijkstra(int s){
  int n=G.size();
  vector<ll> dist(n,INF);
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;

    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  return dist;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n,m,p,q,t;
  cin>>n>>m>>p>>q>>t;
  p--,q--;
  
  G.resize(n);
  rep(i,m){
    int a,b;
    ll c;
    cin>>a>>b>>c;
    a--,b--;
    G[a].push_back(Edge(b,c));
    G[b].push_back(Edge(a,c));
  }
  
  vector<ll> dp=dijkstra(p);
  vector<ll> dq=dijkstra(q);
  vector<ll> d0=dijkstra(0);
  ll ans=-1;
  if(d0[p]+dp[q]+d0[q]<=t){
    cout<<t<<endl;
    return 0;
  }
  rep(i,n) rep(j,n){
    ll tmp=max(dp[i]+dp[j],dq[i]+dq[j]);
    if(d0[i]+d0[j]+tmp<=t) ans=max(ans,t-tmp);
  }
  cout<<ans<<endl;
  
  return 0;
}
0