結果
| 問題 |
No.788 トラックの移動
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-19 00:27:06 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 458 ms / 2,000 ms |
| コード長 | 7,541 bytes |
| コンパイル時間 | 1,919 ms |
| コンパイル使用メモリ | 210,380 KB |
| 最終ジャッジ日時 | 2025-01-06 21:15:09 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 |
ソースコード
#include <bits/stdc++.h>
//!===========================================================!//
//! dP dP dP !//
//! 88 88 88 !//
//! 88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b. !//
//! 88 88 88ooood8 88' `88 88' `88 88ooood8 88' `88 !//
//! 88 88 88. ... 88. .88 88. .88 88. ... 88 !//
//! dP dP `88888P' `88888P8 `88888P8 `88888P' dP !//
//!===========================================================!//
using ld = long double;
using ll = long long;
std::mt19937 mt{std::random_device{}()};
template <typename F>
constexpr F PI() { return 3.1415926535897932385; }
template <typename T, std::size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
template <typename T, typename A>
std::ostream& operator<<(std::ostream& os, const std::deque<T, A>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
template <typename K, typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::multimap<K, T, C, A>& v)
{
os << "[";
for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
return (os << "]" << std::endl);
}
template <typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::multiset<T, C, A>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
template <typename K, typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::map<K, T, C, A>& v)
{
os << "[";
for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
return (os << "]" << std::endl);
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) { return (os << "<" << v.first << "," << v.second << ">"); }
template <typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::set<T, C, A>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
template <typename K, typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_multimap<K, T, H, P, A>& v)
{
os << "[";
for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
return (os << "]" << std::endl);
}
template <typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_multiset<T, H, P, A>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
template <typename K, typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_map<K, T, H, P, A>& v)
{
os << "[";
for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
return (os << "]" << std::endl);
}
template <typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_set<T, H, P, A>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
template <typename T, typename A>
std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& v)
{
os << "[";
for (const auto& e : v) { os << e << ","; }
return (os << "]" << std::endl);
}
#define show(x) std::cerr << #x << " = " << (x) << std::endl
//!=======================================================================================!//
//! a88888b. dP .88888. dP !//
//! d8' `88 88 d8' `88 88 !//
//! 88 .d8888b. .d8888b. d8888P 88 88d888b. .d8888b. 88d888b. 88d888b. !//
//! 88 88' `88 Y8ooooo. 88 88 YP88 88' `88 88' `88 88' `88 88' `88 !//
//! Y8. .88 88. .88 88 88 Y8. .88 88 88. .88 88. .88 88 88 !//
//! Y88888P' `88888P' `88888P' dP `88888' dP `88888P8 88Y888P' dP dP !//
//! 88 !//
//! dP !//
//!=======================================================================================!//
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>
constexpr T INF() { return std::numeric_limits<T>::max() / 4; }
//!==============================================================!//
//! 888888ba oo oo dP dP !//
//! 88 `8b 88 88 !//
//! 88 88 dP dP 88 .dP .d8888b. d8888P 88d888b. .d8888b. !//
//! 88 88 88 88 88888" Y8ooooo. 88 88' `88 88' `88 !//
//! 88 .8P 88 88 88 `8b. 88 88 88 88. .88 !//
//! 8888888P dP 88 dP `YP `88888P' dP dP `88888P8 !//
//! 88 !//
//! dP !//
//!==============================================================!//
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;
}