結果

問題 No.848 なかよし旅行
ユーザー 👑 emthrmemthrm
提出日時 2019-07-05 23:55:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 3,411 bytes
コンパイル時間 1,407 ms
コンパイル使用メモリ 138,912 KB
実行使用メモリ 13,924 KB
最終ジャッジ日時 2024-04-25 13:53:44
合計ジャッジ時間 2,987 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
13,924 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 19 ms
6,940 KB
testcase_12 AC 23 ms
6,948 KB
testcase_13 AC 30 ms
8,192 KB
testcase_14 AC 14 ms
6,944 KB
testcase_15 AC 31 ms
8,064 KB
testcase_16 AC 49 ms
11,136 KB
testcase_17 AC 35 ms
9,088 KB
testcase_18 AC 19 ms
6,940 KB
testcase_19 AC 18 ms
6,940 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 41 ms
9,600 KB
testcase_22 AC 38 ms
10,496 KB
testcase_23 AC 10 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 51 ms
11,136 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
  inline bool operator>(const Edge &rhs) const {
    return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
  }
  inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};

struct Dijkstra {
  using Pci = pair<CostType, int>;

  Dijkstra(const vector<vector<Edge> > &graph, const CostType CINF = LINF) : graph(graph), CINF(CINF) {}

  vector<CostType> build(int s) {
    int n = graph.size();
    vector<CostType> dist(n, CINF);
    dist[s] = 0;
    prev.assign(n, -1);
    priority_queue<Pci, vector<Pci>, greater<Pci> > que;
    que.emplace(0, s);
    while (!que.empty()) {
      Pci pr = que.top(); que.pop();
      int ver = pr.second;
      if (dist[ver] < pr.first) continue;
      for (Edge e : graph[ver]) {
        if (dist[e.dst] > dist[ver] + e.cost) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    return dist;
  }

  vector<int> build_path(int t) {
    vector<int> res;
    for (; t != -1; t = prev[t]) res.emplace_back(t);
    reverse(ALL(res));
    return res;
  }

private:
  vector<vector<Edge> > graph;
  const CostType CINF;
  vector<int> prev;
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m, p, q, t; cin >> n >> m >> p >> q >> t; --p; --q;
  vector<vector<Edge> > graph(n);
  while (m--) {
    int a, b, c; cin >> a >> b >> c; --a; --b;
    graph[a].emplace_back(Edge(a, b, c));
    graph[b].emplace_back(Edge(b, a, c));
  }
  Dijkstra dij(graph);
  vector<long long> dist_0 = dij.build(0), dist_p = dij.build(p), dist_q = dij.build(q);
  if (dist_0[p] * 2 > t || dist_0[q] * 2 > t) {
    cout << -1 << '\n';
    return 0;
  }
  if (dist_0[p] + dist_p[q] + dist_0[q] <= t) {
    cout << t << '\n';
    return 0;
  }
  long long ans = 0;
  REP(i, n) REP(j, n) {
    long long with = dist_0[i] + dist_0[j], sad = max(dist_p[i] + dist_p[j], dist_q[i] + dist_q[j]);
    if (with + sad > t) continue;
    ans = max(ans, t - sad);
  }
  cout << ans << '\n';
  return 0;
}
0