#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll mod = 1000000007; #define rep(i,n) for(int i=0;i=0;i--) #define all(x) (x).begin(),(x).end() typedef pair P; const int INF = 1000000000; struct edge { int to; int cost; }; vector dijkstra(int start, vector> edges) { vector d(edges.size(), INF); //最短距離が小さい順に取り出したいので、 //P.firstを最短距離、P.secondをノード、として、 //greater

を使用。 priority_queue, greater

> costs; costs.push(P(0, start)); d[start] = 0; while (!costs.empty()) { P p = costs.top(); costs.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto e : edges[v]) { int cost = d[v] + e.cost; if (cost < d[e.to]) { d[e.to] = cost; costs.push(P(cost, e.to)); } } } return d; } int main() { int N, M, P, Q, T; cin >> N >> M >> P >> Q >> T; vector> edges(N + 1, vector()); rep(i, M) { int a, b, c; cin >> a >> b >> c; edges[a].emplace_back(edge{ b, c }); edges[b].emplace_back(edge{ a, c }); } auto from1 = dijkstra(1, edges); auto fromP = dijkstra(P, edges); auto fromQ = dijkstra(Q, edges); //2人で一緒に回ることができるか確認 //1→P→Q→1のルートが制限時間内に終わるか? if (from1[P] + fromP[Q] + from1[Q] <= T) { cout << T << endl; return 0; } //2人で間に合わない場合は別々に行く。 //P・Qへ最短ルートで行く。 //1からP・Qそれぞれを最短ルートで往復して間に合わない場合 if (2 * max(from1[P], from1[Q]) > T) { cout << -1 << endl; return 0; } //上記以外は2人が別々に行動する。 //以下の全探索で計算可能。 //一人目:1→x→P→y→1 //二人目:1→x→Q→y→1 ll maxCost = -1; reple(x, 1, N) { reple(y, 1, N) { //一緒に行動する時間 ll cost1 = from1[x] + from1[y]; //別行動する時間(最大) ll cost2 = max(fromP[x] + fromP[y], fromQ[x] + fromQ[y]); if (cost1 + cost2 <= T) { //T = 一緒に行動する時間 + 別行動する時間 + それ以外(同じ場所で待つ時間)なので、 //一緒に行動する時間 + それ以外(同じ場所で待つ時間) = T - 別行動する時間 maxCost = max(maxCost, T - cost2); } } } cout << maxCost << endl; return 0; }