結果
問題 | No.848 なかよし旅行 |
ユーザー | tkmst201 |
提出日時 | 2021-02-24 22:45:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 118 ms / 2,000 ms |
コード長 | 1,655 bytes |
コンパイル時間 | 2,993 ms |
コンパイル使用メモリ | 204,704 KB |
最終ジャッジ日時 | 2025-01-19 04:20:47 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:23:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 23 | scanf("%d %d %d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N, M, P, Q, T; cin >> N >> M >> P >> Q >> T; --P; --Q; vector<vector<pii>> g(N); REP(i, M) { int a, b, c; scanf("%d %d %d", &a, &b, &c); --a; --b; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } auto dijkstra = [&](int s) { vector<ll> dist(N, longINF); using P = pair<ll, int>; priority_queue<P, vector<P>, greater<P>> pq; dist[s] = 0; pq.emplace(0, s); while (!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if (dist[u] != d) continue; for (auto [v, t] : g[u]) if (chmin(dist[v], d + t)) pq.emplace(dist[v], v); } return dist; }; auto dist1 = dijkstra(0), distp = dijkstra(P), distq = dijkstra(Q); if (dist1[P] + distp[Q] + distq[0] <= T) { cout << T << endl; return 0; } if (2 * dist1[P] > T || 2 * dist1[Q] > T) { puts("-1"); return 0; } ll ans = 0; REP(i, N) REP(j, N) { ll a = dist1[i] + distp[i] + distp[j]; ll b = dist1[i] + distq[i] + distq[j]; ll t = max(a, b) + dist1[j]; if (t <= T) chmax(ans, dist1[i] + dist1[j] + T - t); } cout << ans << endl; }