結果

問題 No.848 なかよし旅行
ユーザー betit0919betit0919
提出日時 2019-07-07 19:47:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 2,037 bytes
コンパイル時間 982 ms
コンパイル使用メモリ 111,604 KB
実行使用メモリ 10,524 KB
最終ジャッジ日時 2023-08-07 19:48:33
合計ジャッジ時間 2,720 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
10,524 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 17 ms
5,044 KB
testcase_12 AC 22 ms
5,464 KB
testcase_13 AC 28 ms
6,340 KB
testcase_14 AC 13 ms
4,508 KB
testcase_15 AC 27 ms
6,236 KB
testcase_16 AC 43 ms
7,996 KB
testcase_17 AC 30 ms
6,940 KB
testcase_18 AC 16 ms
5,004 KB
testcase_19 AC 15 ms
4,568 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 35 ms
7,096 KB
testcase_22 AC 34 ms
7,480 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 46 ms
7,832 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 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, INFLL);
  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