結果
問題 | No.848 なかよし旅行 |
ユーザー |
![]() |
提出日時 | 2020-04-14 15:52:26 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 176 ms / 2,000 ms |
コード長 | 3,460 bytes |
コンパイル時間 | 1,953 ms |
コンパイル使用メモリ | 143,076 KB |
最終ジャッジ日時 | 2025-01-09 18:41:34 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <algorithm> #include <cmath> #include <complex> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #include <functional> #include <cstring> #include <regex> #include <random> #include <cassert> #include <stack> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, s, n) for (int i = (s); i < (int)(n); i++) #define revrep(i, n) for (int i = (n) - 1; i >= 0; i--) #define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--) #define debug(x) cerr << #x << ": " << x << "\n" #define popcnt(x) __builtin_popcount(x) using ll = long long; using P = pair<int, int>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> istream& operator >>(istream &is, vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i); return is; } template<class T, class U> ostream& operator <<(ostream &os, pair<T, U> p) { cout << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; } template<class T> void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); } template<class Weight> struct WeightedGraph { struct Edge{ int to; Weight weight; }; int n; vector<vector<Edge>> edges; Weight zero; Weight inf; WeightedGraph(int n, Weight zero, Weight inf) : n(n), edges(n), zero(zero), inf(inf) {} void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); } }; template<class Weight> vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g/*, vector<Weight> &d*/) { using Node = pair<Weight, int>; priority_queue<Node, vector<Node>, greater<Node>> que; vector<Weight> d(g.n, g.inf); d[s] = g.zero; que.push(Node(g.zero, s)); while (!que.empty()) { Node p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < (int)g.edges[v].size(); i++) { auto e = g.edges[v][i]; if (d[e.to] > d[v] + e.weight) { d[e.to] = d[v] + e.weight; que.push(Node(d[e.to], e.to)); } } } return d; } int main() { int n, m, p, q, t; cin >> n >> m >> p >> q >> t; p--; q--; WeightedGraph<ll> graph(n, 0, 1e18); rep(i, m) { int a, b, w; cin >> a >> b >> w; a--; b--; graph.add_edge(a, b, w); graph.add_edge(b, a, w); } auto dist_p_to = dijkstra(p, graph); auto dist_q_to = dijkstra(q, graph); auto dist_0_to = dijkstra(0, graph); if (dist_0_to[p] + dist_p_to[q] + dist_q_to[0] <= t || dist_0_to[q] + dist_q_to[p] + dist_p_to[0] <= t) { cout << t << endl; return 0; } ll ans = -1; /*rep(v, n) { ll solo = max(dist_p_to[v] * 2, dist_q_to[v] * 2); if (dist_0_to[v] * 2 + solo <= t) { chmax(ans, t - solo); } }*/ rep(u, n) rep(v, n) { ll solo = max(dist_p_to[u] + dist_p_to[v], dist_q_to[u] + dist_q_to[v]); if (dist_0_to[u] + solo + dist_0_to[v] <= t) chmax(ans, t - solo); } cout << ans << endl; }