結果

問題 No.848 なかよし旅行
ユーザー roto_37roto_37
提出日時 2019-07-23 19:45:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 5,076 bytes
コンパイル時間 1,117 ms
コンパイル使用メモリ 125,808 KB
実行使用メモリ 10,564 KB
最終ジャッジ日時 2023-08-07 19:49:50
合計ジャッジ時間 2,803 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
10,564 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 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 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 17 ms
5,260 KB
testcase_12 AC 22 ms
5,508 KB
testcase_13 AC 29 ms
6,396 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 27 ms
6,260 KB
testcase_16 AC 43 ms
8,060 KB
testcase_17 AC 31 ms
6,924 KB
testcase_18 AC 16 ms
5,116 KB
testcase_19 AC 15 ms
4,664 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 35 ms
7,052 KB
testcase_22 AC 34 ms
7,256 KB
testcase_23 AC 19 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 47 ms
7,832 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <bits/stdc++.h>

#include <iostream>
#include <algorithm>

#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <cstring>
#include <string>
#include <utility>
#include <array>
#include <complex>
#include <valarray>

#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <chrono>
#include <random>
#include <numeric>

#include<memory>


using namespace std;
#define int long long

typedef long long ll;
typedef unsigned long long ull;
//typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double,int> pdi;
typedef pair<long double, int> pdbi;
typedef pair<int,pii> pipii;
typedef pair<ll,pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1<<30;
const ll INFLL = 1LL<<62;
const long double EPS = 1e-12;
const ll mod = 1000000000 + 7;
//const ll mod = 998244353;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const long double pi = 3.141592653589793;

#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod
#define inside(y, x, h, w) (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false

//debug
#define DEBUG

#define DUMPOUT cerr

#ifdef DEBUG
#define dump(...) DUMPOUT<<#__VA_ARGS__<<" :["<<__FUNCTION__<<":"<<__LINE__<<"]"<<endl; DUMPOUT<<"    "; dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

void dump_func() {DUMPOUT << endl;};

template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) == 0) DUMPOUT << " ";
    else DUMPOUT << ", ";
    dump_func(std::move(tail)...);
}

//ostream
template<typename T> ostream& operator << (ostream& os, vector<T>& vec) {
    os << "["; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]";
    return os;
}

template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}

template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& map_var) {
    os << "[";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")"; itr++;  if(itr != map_var.end()) os << ", "; itr--;
    }
    os << "]";
    return os;
}

template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {
    os << "[";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr; ++itr; if(itr != set_var.end()) os << ", "; itr--;
    }
    os << "]";
    return os;
}

int n, m, p, q, t;

struct edge{int to, cost;};
vector<vector<edge>> g(2000 + 5);

vector<ll> dijkstra(int s, int n, vector<vector<edge>>& g){
    priority_queue<plli, vector<plli>, greater<plli>> pque;
    vector<ll> d(n, INFLL);
    d[s] = 0;
    pque.push(plli(0, s));

    while (!pque.empty()){
        plli p = pque.top(); pque.pop();
        int fr = p.second;
        if (d[fr] < p.first) continue;
        for (edge e: g[fr]){
            if (d[e.to] > d[fr] + e.cost){
                d[e.to] = d[fr] + e.cost;
                pque.push(plli(d[e.to], e.to));
            }
        }
    }
    return d;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m >> p >> q >> t;
  p--; q--;
  rep(i, m) {
    int u, v, c;
    cin >> u >> v >> c;
    u--; v--;
    g[u].push_back(edge{v, c});
    g[v].push_back(edge{u, c});
  }
  vector<int> d1 = dijkstra(0, n, g);
  if (2 * d1[p] > t || 2 * d1[q] > t) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> dp = dijkstra(p, n, g);
  vector<int> dq = dijkstra(q, n, g);
  if (d1[p] + dp[q] + dq[0] <= t) {
    cout << t << endl;
    return 0;
  }
  vector<pair<int, pii>> GO(n);
  vector<pair<int, pii>> BACK(n);
  rep(i, n) {
    GO[i] = {d1[i], {d1[i] + dp[i], d1[i] + dq[i]}};
    BACK[i] ={d1[i], {dp[i], dq[i]}};
  }
  int ans = -1;
  rep(i, n) {
    if (i == p || i == q) continue;
    int yuki1 = GO[i].se.fi;
    int yuki2 = GO[i].se.se;
    int Same = GO[i].fi;
    rep(j, n) {
      int tmp_yuki1 = yuki1 + BACK[j].se.fi;
      int tmp_yuki2 = yuki2 + BACK[j].se.se;
      int tmpT = d1[j] + max(tmp_yuki1, tmp_yuki2);
      if (tmpT <= t) {
        ans = max(ans, Same + d1[j] + t - tmpT);
      }
    }
  }
  cout << ans << endl;
}
0