結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-07-13 14:59:13 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 76 ms / 2,000 ms |
| コード長 | 2,104 bytes |
| コンパイル時間 | 1,359 ms |
| コンパイル使用メモリ | 90,060 KB |
| 実行使用メモリ | 10,588 KB |
| 最終ジャッジ日時 | 2024-11-08 01:00:04 |
| 合計ジャッジ時間 | 2,653 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using i64 = int64_t;
struct edge { int to; i64 cost; };
constexpr i64 inf = 987'654'321'987'654'321LL;
template <class T>
inline void readint(T *x) {
*x = 0;
int c;
for(;;) {
c = getchar_unlocked();
switch(c) case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: goto num;
}
num:
for(;;) {
switch(c) {
case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
*x *= 10;
*x += c - 48;
break;
default:
return;
}
c = getchar_unlocked();
}
}
int N;
vector<vector<edge>> G;
vector<i64> dijk(int s) {
vector<i64> cost(N, inf);
cost[s] = 0;
priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> pq;
pq.emplace(0, s);
vector<bool> seen(N, false);
while(!pq.empty()) {
i64 _; int v; tie(_, v) = pq.top(); pq.pop();
if(seen[v]) { continue; }
seen[v] = true;
for(edge &e : G[v]) {
if(cost[e.to] > cost[v] + e.cost) {
cost[e.to] = cost[v] + e.cost;
pq.emplace(cost[e.to], e.to);
}
}
}
return cost;
}
int main(void) {
int M, P, Q; i64 T;
readint(&N);
readint(&M);
readint(&P);
readint(&Q);
readint(&T);
G.assign(N, vector<edge>());
--P; --Q;
for(int i=0; i<M; ++i) {
int a, b, c;
readint(&a);
readint(&b);
readint(&c);
--a; --b;
G[a].push_back(edge{b, c});
G[b].push_back(edge{a, c});
}
vector<i64> c0 = dijk(0),
c1 = dijk(P),
c2 = dijk(Q);
// 0 -> P -> Q -> 0
if(c0[P] + c1[Q] + c0[Q] <= T) {
printf("%ld\n", T);
return 0;
}
// 0 -> x -> P -> y -> 0
// Q
i64 maxi = -1;
for(int x=0; x<N; ++x) {
for(int y=0; y<N; ++y) {
i64 r1 = c0[x] + c1[x] + c1[y] + c0[y],
r2 = c0[x] + c2[x] + c2[y] + c0[y];
i64 rr = max(r1, r2);
if(rr <= T) {
maxi = max(maxi, T - rr + c0[x] + c0[y]);
}
}
}
printf("%ld\n", maxi);
return 0;
}