結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
koi_kotya
|
| 提出日時 | 2019-07-06 04:42:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 133 ms / 2,000 ms |
| コード長 | 1,880 bytes |
| コンパイル時間 | 1,690 ms |
| コンパイル使用メモリ | 176,028 KB |
| 実行使用メモリ | 7,092 KB |
| 最終ジャッジ日時 | 2024-11-08 00:56:46 |
| 合計ジャッジ時間 | 3,821 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define p_ary(ary,a,b,i) do { cout << "["; for (int (i) = (a);(i) < (b);++(i)) cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)
int MAX_N = 2010;
ll INF = 1LL<<60;
vector<vector<P>> G(MAX_N);
void dijkstra(int s,vector<ll>& dist) {
priority_queue<P,vector<P>,greater<P>> que;
dist[s] = 0;
que.push(P(0,s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0;i < G[v].size();++i) {
P e = G[v][i];
if (dist[e.first] > dist[v]+e.second) {
dist[e.first] = dist[v]+e.second;
que.push(P(dist[e.first],e.first));
}
}
}
}
int main() {
int n,m,p,q,t;
cin >> n >> m >> p >> q >> t;
for (int i = 0;i < m;++i) {
int u,v,w;
cin >> u >> v >> w;
u--;v--;
G[u].push_back(P(v,w));
G[v].push_back(P(u,w));
}
p--;q--;
vector<ll> dist1(n,INF),dist2(n,INF),dist3(n,INF);
dijkstra(0,dist1);
dijkstra(p,dist2);
dijkstra(q,dist3);
if (dist1[p]*2 > t || dist1[q]*2 > t) {
cout << -1 << endl;
return 0;
}
if (dist1[p]+dist2[q]+dist3[0] <= t) {
cout << t << endl;
return 0;
}
ll ans = 0;
for (int i = 0;i < n;++i) {
for (int j = 0;j < n;++j) {
ll d = max(dist2[i]+dist2[j],dist3[i]+dist3[j]);
if (d+dist1[i]+dist1[j] > t) continue;
ans = max(ans,t-d);
}
}
cout << ans << endl;
return 0;
}
koi_kotya