結果

問題 No.848 なかよし旅行
ユーザー pekempey
提出日時 2019-07-05 22:06:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 1,421 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 178,640 KB
実行使用メモリ 10,620 KB
最終ジャッジ日時 2024-11-08 00:48:51
合計ジャッジ時間 3,500 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

struct edge {
  int v;
  ll w;
};

vector<ll> distance(const vector<vector<edge>> &g, int s) {
  const int n = g.size();
  priority_queue<pair<ll, int>> q;
  q.emplace(0, s);
  vector<ll> dist(n, 1e18);
  dist[s] = 0;
  while (!q.empty()) {
    int u = q.top().second;
    ll d = -q.top().first;
    q.pop();
    if (dist[u] < d) continue;
    for (edge e : g[u]) {
      if (dist[e.v] > dist[u] + e.w) {
        dist[e.v] = dist[u] + e.w;
        q.emplace(-dist[e.v], e.v);
      }
    }
  }
  return dist;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, M, P, Q;
  ll T;
  cin >> N >> M >> P >> Q >> T;
  P--; Q--;
  vector<vector<edge>> g(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    g[a].push_back((edge){b, c});
    g[b].push_back((edge){a, c});
  }
  vector<ll> distP = distance(g, P);
  vector<ll> distQ = distance(g, Q);
  vector<ll> dist0 = distance(g, 0);
  ll ans = -1;
  if (dist0[P] + distP[Q] + distQ[0] <= T) {
    ans = max(ans, T);
  }
  rep(i, N) {
    rep(j, N) {
      if (dist0[i] + dist0[j] + max(distP[i] + distP[j], distQ[i] + distQ[j]) <= T) {
        ans = max(ans, T - max(distP[i] + distP[j], distQ[i] + distQ[j]));
      }
    }
  }
  cout << ans << endl;
}
0