結果
問題 |
No.848 なかよし旅行
|
ユーザー |
![]() |
提出日時 | 2020-06-14 19:54:37 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 168 ms / 2,000 ms |
コード長 | 2,244 bytes |
コンパイル時間 | 1,624 ms |
コンパイル使用メモリ | 173,920 KB |
実行使用メモリ | 18,196 KB |
最終ジャッジ日時 | 2024-11-08 01:16:31 |
合計ジャッジ時間 | 4,048 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
/** * @FileName a.cpp * @Author kanpurin * @Created 2020.06.14 19:54:32 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; template < typename T > struct Dijkstra { private: int V; struct edge { int to; T cost; }; vector< vector< edge > > G; public: const T inf = numeric_limits< T >::max(); vector< T > d; Dijkstra(int V) : V(V) { G.resize(V); } void add_edge(int from, int to, T weight, bool directed = false) { G[from].push_back({to, weight}); if (!directed) G[to].push_back({from, weight}); } void build(int s) { d.assign(V, inf); typedef tuple< T, int > P; priority_queue< P, vector< P >, greater< P > > pq; d[s] = 0; pq.push(P(d[s], s)); while (!pq.empty()) { P p = pq.top(); pq.pop(); int v = get< 1 >(p); if (d[v] < get< 0 >(p)) continue; for (const edge &e : G[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; pq.push(P(d[e.to], e.to)); } } } } }; int main() { int n, m, p, q, t; cin >> n >> m >> p >> q >> t; p--; q--; Dijkstra< ll > g1(n), gp(n), gq(n); for (int i = 0; i < m; i++) { int u, v, c; cin >> u >> v >> c; u--; v--; g1.add_edge(u, v, c); gp.add_edge(u, v, c); gq.add_edge(u, v, c); } g1.build(0); gp.build(p); gq.build(q); if (g1.d[p] + gp.d[q] + gq.d[0] <= t) { cout << t << endl; return 0; } ll ans = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ll t11 = g1.d[i]; ll tp1 = gp.d[i]; ll tq1 = gq.d[i]; ll t12 = g1.d[j]; ll tp2 = gp.d[j]; ll tq2 = gq.d[j]; if (t11 + tp1 + tp2 + t12 > t) continue; if (t11 + tq1 + tq2 + t12 > t) continue; ans = max(ans,t - max(tp1 + tp2,tq1 + tq2)); } } cout << ans << endl; return 0; }