結果

問題 No.848 なかよし旅行
ユーザー playrollerplayroller
提出日時 2019-07-16 09:40:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 594 ms / 2,000 ms
コード長 1,873 bytes
コンパイル時間 1,777 ms
コンパイル使用メモリ 173,916 KB
実行使用メモリ 9,416 KB
最終ジャッジ日時 2023-08-07 19:49:39
合計ジャッジ時間 4,826 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 594 ms
7,396 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 30 ms
5,408 KB
testcase_12 AC 38 ms
5,900 KB
testcase_13 AC 46 ms
6,716 KB
testcase_14 AC 22 ms
4,376 KB
testcase_15 AC 46 ms
6,564 KB
testcase_16 AC 71 ms
8,320 KB
testcase_17 AC 44 ms
7,444 KB
testcase_18 AC 23 ms
5,108 KB
testcase_19 AC 20 ms
4,568 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 50 ms
7,344 KB
testcase_22 AC 38 ms
7,480 KB
testcase_23 AC 9 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 86 ms
9,416 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,j,n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define LINF 1e18
const int mod = 1e9 + 7;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));}
i64 lcd(i64 n, i64 m) {return (n / gcd(n, m) * m);}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

vt<i64> dijkstra(int s, vvt<pair<int, i64>> &edge, int n) {
  vt<i64> dist(n + 1, INF);
  dist[s] = 0;

  priority_queue<pair<int, i64>, vt<pair<int, i64>>, greater<pair<int, i64>>> que;
  que.push({s, 0});
  while(!que.empty()) {
    int v = que.top().first;
    i64 c = que.top().second;
    que.pop();

    if(c > dist[v]) continue;
    for(auto &nxt : edge[v]) {
      if(dist[v] + nxt.second < dist[nxt.first]) {
        dist[nxt.first] = dist[v] + nxt.second;
        que.push({nxt.first, dist[nxt.first]});
      }
    }
  }
  return dist;
}

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

  int n, m, p, q, t;
  cin >> n >> m >> p >> q >> t;
  vvt<pair<int, i64>> edge(n + 1);
  rep(i, 0, m) {
    int a, b, c;
    cin >> a >> b >> c;
    edge[a].push_back({b, c});
    edge[b].push_back({a, c});
  }

  auto dist = dijkstra(1, edge, n);
  auto distP = dijkstra(p, edge, n);
  auto distQ = dijkstra(q, edge, n);

  if(dist[p] + distP[q] + distQ[1] <= t) cout << t << endl;
  else if(max(2 * dist[p], 2 * dist[q]) > t) cout << -1 << endl;
  else {
    i64 ans = 0;
    rep(x, 1, n + 1) {
      rep(y, 1, n + 1) {
        if(dist[x] + max(distP[x] + distP[y], distQ[x] + distQ[y]) + dist[y] <= t) {
          ans = max(ans, t - max(distP[x] + distP[y], distQ[x] + distQ[y]));
        }
      }
    }
    cout << ans << endl;
  }
}
0