結果

問題 No.848 なかよし旅行
ユーザー betit0919betit0919
提出日時 2019-07-07 19:47:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 2,035 bytes
コンパイル時間 1,130 ms
コンパイル使用メモリ 111,304 KB
実行使用メモリ 10,632 KB
最終ジャッジ日時 2024-04-25 13:56:03
合計ジャッジ時間 2,510 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
10,632 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 17 ms
6,940 KB
testcase_12 AC 22 ms
6,940 KB
testcase_13 AC 29 ms
6,944 KB
testcase_14 AC 12 ms
6,940 KB
testcase_15 AC 27 ms
6,940 KB
testcase_16 AC 44 ms
8,064 KB
testcase_17 AC 31 ms
7,040 KB
testcase_18 AC 17 ms
6,944 KB
testcase_19 AC 15 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 36 ms
7,168 KB
testcase_22 AC 34 ms
7,296 KB
testcase_23 AC 9 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 48 ms
7,936 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,948 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <map>
#include <numeric>
#include <random>
#include <queue>
#include <deque>
#include <tuple>
#include <iomanip>

using namespace std;
typedef long long ll;

const int INF = (1 << 30) - 1;
const ll INFLL= (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()

template<class T = ll>
struct Edge {
  int to;
  T cost;
  Edge() {}
  Edge(int to, T cost) : to(to), cost(cost) {}
  bool operator>(const Edge &r) const { return this->cost > r.cost; }
};

template<class T = ll>
vector<T> Dijkstra(vector<vector<Edge<T>>> &edges, int st) {

  int V = (int)edges.size();
  vector<T> dist(V, INF);
  dist[st] = 0;

  priority_queue<Edge<T>, vector<Edge<T>>, greater<Edge<T>>> p_que;
  p_que.emplace(st, dist[st]);

  while (!p_que.empty()) {

    Edge<T> now(p_que.top().to, p_que.top().cost);
    p_que.pop();

    if (dist[now.to] < now.cost) continue;
    for (const Edge<T> &e : edges[now.to]) {
      T tmp_cost = now.cost + e.cost;
      if (dist[e.to] > tmp_cost) {
        dist[e.to] = tmp_cost;
        p_que.emplace(e.to, dist[e.to]);
      }
    }
  }

  return dist;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N,M,P,Q,T;
  cin>>N>>M>>P>>Q>>T;
  P--;Q--;
  vector<vector<Edge<ll> > > edges(N);
  for(int i=0;i<M;i++){
    ll a,b,c;
    cin>>a>>b>>c; a--; b--;
    edges[a].emplace_back(b,c);
    edges[b].emplace_back(a,c);
  }
  auto ans1 = Dijkstra(edges,0);
  auto ansq = Dijkstra(edges,Q);
  auto ansp = Dijkstra(edges,P);
  ll p1=ans1[P],q1=ans1[Q],pq=ansq[P];
  if(p1*2>T || q1*2>T){
    cout<<-1<<endl;
  }else if(p1+q1+pq<=T){
    cout<<T<<endl;
  }else{
    ll ans=INFLL;
    for(int i=0;i<N;i++){
      for(int j=0;j<N;j++){
        if((ans1[i]+max(ansp[i]+ansp[j],ansq[i]+ansq[j])+ans1[j])<=T){
          ans=min(ans,max(ansp[i]+ansp[j],ansq[i]+ansq[j]));
        }
      }
    }
    cout<<T-ans<<endl;
  }
}
0