結果
| 問題 |
No.848 なかよし旅行
|
| コンテスト | |
| ユーザー |
YamaKasa
|
| 提出日時 | 2019-07-13 00:34:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 130 ms / 2,000 ms |
| コード長 | 2,148 bytes |
| コンパイル時間 | 1,906 ms |
| コンパイル使用メモリ | 172,940 KB |
| 実行使用メモリ | 12,332 KB |
| 最終ジャッジ日時 | 2024-11-08 01:00:31 |
| 合計ジャッジ時間 | 3,913 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, M, P, Q, T;
static const ll INF = 100000000000000;
struct Node {
int id;
ll cost;
Node() {}
Node(int id, ll cost) : id(id), cost(cost){}
bool operator< (const Node& node) const {
return node.cost < cost;
}
};
// N: 頂点数, E: 辺の数, d: 距離 を格納する配列,
// t: 始点, flag: true なら 無向辺, false なら 有向辺
void dijkstra(int N, int E, ll d[], int t, int from[], int to[], ll cost[], bool flag) {
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int color[N];
vector<Node> adj[N];
for (int i = 0; i < N; i++) {
d[i] = INF;
color[i] = WHITE;
}
// 隣接リストの作成
for (int i = 0; i < E; i++) {
adj[from[i]].push_back(Node(to[i], cost[i]));
if (flag) adj[to[i]].push_back(Node(from[i], cost[i]));
}
priority_queue<Node> pq;
d[t] = 0;
pq.push(Node(t, 0));
while (!pq.empty()) {
Node f = pq.top();
pq.pop();
int u = f.id;
color[u] = BLACK;
if (d[u] < f.cost) continue;
for (int j = 0; j < adj[u].size(); j++) {
int v = adj[u][j].id;
if (color[v] == BLACK) continue;
if (d[v] > d[u] + adj[u][j].cost) {
d[v] = d[u] + adj[u][j].cost;
pq.push(Node(v, d[v]));
color[v] = GRAY;
}
}
}
}
int main() {
cin >> N >> M >> P >> Q >> T;
int a[M], b[M];
ll c[M];
for (int i = 0; i < M; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
}
P--;
Q--;
ll d[N], dP[N], dQ[N];
dijkstra(N, M, d, 0, a, b, c, true);
dijkstra(N, M, dP, P, a, b, c, true);
dijkstra(N, M, dQ, Q, a, b, c, true);
if (d[P] * 2 > T || d[Q] * 2 > T) {
cout << "-1\n";
return 0;
}
if (d[P] + dP[Q] + dQ[0] <= T) {
cout << T << "\n";
return 0;
}
ll ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (d[i] + dP[i] + dP[j] + d[j] > T) continue;
if (d[i] + dQ[i] + dQ[j] + d[j] > T) continue;
ans = max(ans, T - max(dP[i] + dP[j], dQ[i] + dQ[j]));
}
}
cout << ans << "\n";
return 0;
}
YamaKasa