結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
treeone
|
| 提出日時 | 2019-07-05 22:05:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 94 ms / 2,000 ms |
| コード長 | 1,845 bytes |
| コンパイル時間 | 2,082 ms |
| コンパイル使用メモリ | 206,616 KB |
| 最終ジャッジ日時 | 2025-01-07 05:59:09 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;
const int MAX_V = 2010;
struct edge{
int to, cost;
edge(int to, int cost):to(to), cost(cost){}
};
vector<edge> G[MAX_V];
struct Dijkstra{
vector<int> d;
Dijkstra(){}
Dijkstra(int V){
d.resize(V, INF);
}
void calc(int s){
d[s] = 0;
priority_queue<P, vector<P>, greater<P> > q;
q.push(P(d[s], s));
while(!q.empty()){
P p = q.top(); q.pop();
int from = p.second;
int cost = p.first;
if(d[from] < cost) continue;
rep(i, 0, G[from].size()){
int next = G[from][i].to;
int newCost = cost + G[from][i].cost;
if(d[next] > newCost){
d[next] = newCost;
q.push(P(newCost, next));
}
}
}
}
};
signed main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m, p, q, t;
cin >> n >> m >> p >> q >> t;
p--; q--;
rep(i, 0, m){
int a, b, c;
cin >> a >> b >> c;
a--; b--;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
Dijkstra dp(n), dq(n), ds(n);
dp.calc(p);
dq.calc(q);
ds.calc(0);
int ans = -1;
if(ds.d[p] + dp.d[q] + ds.d[q] <= t){
ans = t;
}
rep(i, 0, n){
rep(j, 0, n){
int tp = ds.d[i] + dp.d[i] + dp.d[j] + ds.d[j];
int tq = ds.d[i] + dq.d[i] + dq.d[j] + ds.d[j];
if(tp <= t && tq <= t){
int tmp = ds.d[i] + ds.d[j] + (t - max(tp, tq));
ans = max(ans, tmp);
}
}
}
cout << ans << endl;
}
treeone