結果
| 問題 |
No.788 トラックの移動
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-09 17:40:17 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 534 ms / 2,000 ms |
| コード長 | 2,352 bytes |
| コンパイル時間 | 1,024 ms |
| コンパイル使用メモリ | 88,140 KB |
| 最終ジャッジ日時 | 2025-01-06 21:05:29 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 |
ソースコード
#include <iostream>
using ll = long long;
#include <limits>
template <typename T>
constexpr T INF() { return std::numeric_limits<T>::max() / 16; }
#include <queue>
#include <vector>
template <typename T>
struct CostGraph
{
CostGraph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}
struct Edge
{
Edge(const std::size_t from, const std::size_t to, const T cost) : from{from}, to{to}, cost{cost} {}
const std::size_t from, to;
const T cost;
bool operator<(const Edge& e) const { return cost != e.cost ? cost < e.cost : to < e.to; }
};
void addEdge(const std::size_t from, const std::size_t to, const T cost) { edge[from].push_back(Edge{from, to, cost}), rev_edge[to].push_back(Edge(to, from, cost)); }
const std::size_t V;
std::vector<std::vector<Edge>> edge, rev_edge;
};
template <typename T>
std::vector<T> Dijkstra(const CostGraph<T>& g, const std::size_t s)
{
std::vector<T> d(g.V, INF<T>());
using P = std::pair<T, std::size_t>;
std::priority_queue<P, std::vector<P>, std::greater<P>> q;
d[s] = 0, q.push({0, s});
while (not q.empty()) {
const T cost = q.top().first;
const std::size_t v = q.top().second;
q.pop();
if (d[v] < cost) { continue; }
for (const auto& e : g.edge[v]) {
if (d[e.to] <= d[v] + e.cost) { continue; }
d[e.to] = d[v] + e.cost, q.push({d[e.to], e.to});
}
}
return d;
}
int main()
{
int N, M, L;
std::cin >> N >> M >> L, L--;
CostGraph<ll> g(N);
std::vector<ll> t(N);
ll tsum = 0;
for (int i = 0; i < N; i++) { std::cin >> t[i], tsum += t[i]; }
for (int i = 0, a, b, c; i < M; i++) { std::cin >> a >> b >> c, a--, b--, g.addEdge(a, b, c), g.addEdge(b, a, c); }
for (int i = 0; i < N; i++) {
if (t[i] == tsum) { return std::cout << 0 << std::endl, 0; }
}
std::vector<std::vector<ll>> d(N, std::vector<ll>(N));
for (int i = 0; i < N; i++) { d[i] = Dijkstra(g, i); }
ll ans = INF<ll>();
for (int i = 0; i < N; i++) {
ll sum = 0;
for (int j = 0; j < N; j++) { sum += 2 * d[i][j] * t[j]; }
for (int j = 0; j < N; j++) {
if (t[j] == 0) { continue; }
ans = std::min(ans, sum + d[L][j] - d[i][j]);
}
}
std::cout << ans << std::endl;
return 0;
}