結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2019-07-05 22:40:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,369 bytes |
| コンパイル時間 | 1,698 ms |
| コンパイル使用メモリ | 135,984 KB |
| 最終ジャッジ日時 | 2025-01-07 06:04:35 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 5 WA * 13 TLE * 8 |
ソースコード
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
int src, dst; CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &rhs) const {
return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
}
inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
inline bool operator>(const Edge &rhs) const {
return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
}
inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};
struct Dijkstra {
using Pci = pair<CostType, int>;
Dijkstra(const vector<vector<Edge> > &graph, const CostType CINF = LINF) : graph(graph), CINF(CINF) {}
vector<CostType> build(int s) {
int n = graph.size();
vector<CostType> dist(n, CINF);
dist[s] = 0;
prev.assign(n, -1);
priority_queue<Pci, vector<Pci>, greater<Pci> > que;
que.emplace(0, s);
while (!que.empty()) {
Pci pr = que.top(); que.pop();
int ver = pr.second;
if (dist[ver] < pr.first) continue;
for (Edge e : graph[ver]) {
if (dist[e.dst] > dist[ver] + e.cost) {
dist[e.dst] = dist[ver] + e.cost;
prev[e.dst] = ver;
que.emplace(dist[e.dst], e.dst);
}
}
}
return dist;
}
vector<int> build_path(int t) {
vector<int> res;
for (; t != -1; t = prev[t]) res.emplace_back(t);
reverse(ALL(res));
return res;
}
private:
vector<vector<Edge> > graph;
const CostType CINF;
vector<int> prev;
};
int main() {
cin.tie(0); ios::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
int n, m, p, q, t; cin >> n >> m >> p >> q >> t; --p; --q;
vector<vector<Edge> > graph(n);
while (m--) {
int a, b, c; cin >> a >> b >> c; --a; --b;
graph[a].emplace_back(Edge(a, b, c));
graph[b].emplace_back(Edge(b, a, c));
}
Dijkstra dij(graph);
vector<vector<long long> > dist(n);
REP(i, n) dist[i] = dij.build(i);
if (dist[0][p] * 2 > t || dist[0][q] * 2 > t) {
cout << -1 << '\n';
return 0;
}
if (dist[0][p] + dist[p][q] + dist[q][0] <= t) {
cout << t << '\n';
return 0;
}
long long ans = 0;
REP(i, n) {
long long with = dist[0][i], sad = max(dist[i][p], dist[i][q]);
if ((with + sad) * 2 > t) continue;
ans = max(ans, t - sad * 2);
}
cout << ans << '\n';
return 0;
}
emthrm