#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector sort_unique(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl #define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr) #else #define dbg(x) (x) #define dbgif(cond, x) 0 #endif template ::max() / 2, int INVALID = -1> struct ShortestPath { int V, E; bool single_positive_weight; T wmin, wmax; std::vector>> to; ShortestPath(int V = 0) : V(V), E(0), single_positive_weight(true), wmin(0), wmax(0), to(V) {} void add_edge(int s, int t, T w) { assert(0 <= s and s < V); assert(0 <= t and t < V); to[s].emplace_back(t, w); E++; if (w > 0 and wmax > 0 and wmax != w) single_positive_weight = false; wmin = std::min(wmin, w); wmax = std::max(wmax, w); } std::vector dist; std::vector prev; // Dijkstra algorithm // Complexity: O(E log E) void Dijkstra(int s) { assert(0 <= s and s < V); dist.assign(V, INF); dist[s] = 0; prev.assign(V, INVALID); using P = std::pair; std::priority_queue, std::greater

> pq; pq.emplace(0, s); while (!pq.empty()) { T d; int v; std::tie(d, v) = pq.top(); pq.pop(); if (dist[v] < d) continue; for (auto nx : to[v]) { T dnx = d + nx.second; if (dist[nx.first] > dnx) { dist[nx.first] = dnx, prev[nx.first] = v; pq.emplace(dnx, nx.first); } } } } // Dijkstra algorithm, O(V^2 + E) void DijkstraVquad(int s) { assert(0 <= s and s < V); dist.assign(V, INF); dist[s] = 0; prev.assign(V, INVALID); std::vector fixed(V, false); while (true) { int r = INVALID; T dr = INF; for (int i = 0; i < V; i++) { if (!fixed[i] and dist[i] < dr) r = i, dr = dist[i]; } if (r == INVALID) break; fixed[r] = true; int nxt; T dx; for (auto p : to[r]) { std::tie(nxt, dx) = p; if (dist[nxt] > dist[r] + dx) dist[nxt] = dist[r] + dx, prev[nxt] = r; } } } // Bellman-Ford algorithm // Complexity: O(VE) bool BellmanFord(int s, int nb_loop) { assert(0 <= s and s < V); dist.assign(V, INF), prev.assign(V, INVALID); dist[s] = 0; for (int l = 0; l < nb_loop; l++) { bool upd = false; for (int v = 0; v < V; v++) { if (dist[v] == INF) continue; for (auto nx : to[v]) { T dnx = dist[v] + nx.second; if (dist[nx.first] > dnx) dist[nx.first] = dnx, prev[nx.first] = v, upd = true; } } if (!upd) return true; } return false; } // Bellman-ford algorithm using queue (deque) // Complexity: O(VE) // Requirement: no negative loop void SPFA(int s) { assert(0 <= s and s < V); dist.assign(V, INF); prev.assign(V, INVALID); std::deque q; std::vector in_queue(V); dist[s] = 0; q.push_back(s), in_queue[s] = 1; while (!q.empty()) { int now = q.front(); q.pop_front(), in_queue[now] = 0; for (auto nx : to[now]) { T dnx = dist[now] + nx.second; int nxt = nx.first; if (dist[nxt] > dnx) { dist[nxt] = dnx; if (!in_queue[nxt]) { if (q.size() and dnx < dist[q.front()]) { // Small label first optimization q.push_front(nxt); } else { q.push_back(nxt); } prev[nxt] = now, in_queue[nxt] = 1; } } } } } void ZeroOneBFS(int s) { assert(0 <= s and s < V); dist.assign(V, INF), prev.assign(V, INVALID); dist[s] = 0; std::deque que; que.push_back(s); while (!que.empty()) { int v = que.front(); que.pop_front(); for (auto nx : to[v]) { T dnx = dist[v] + nx.second; if (dist[nx.first] > dnx) { dist[nx.first] = dnx, prev[nx.first] = v; if (nx.second) { que.push_back(nx.first); } else { que.push_front(nx.first); } } } } } // Retrieve a sequence of vertex ids that represents shortest path [s, ..., goal] // If not reachable to goal, return {} std::vector retrieve_path(int goal) const { assert(int(prev.size()) == V); assert(0 <= goal and goal < V); if (dist[goal] == INF) return {}; std::vector ret{goal}; while (prev[goal] != INVALID) { goal = prev[goal]; ret.push_back(goal); } std::reverse(ret.begin(), ret.end()); return ret; } void solve(int s) { if (wmin >= 0) { if (single_positive_weight) { ZeroOneBFS(s); } else { if ((long long)V * V < (E << 4)) { DijkstraVquad(s); } else { Dijkstra(s); } } } else { BellmanFord(s, V); } } // Warshall-Floyd algorithm // Complexity: O(E + V^3) std::vector> dist2d; void WarshallFloyd() { dist2d.assign(V, std::vector(V, INF)); for (int i = 0; i < V; i++) { dist2d[i][i] = 0; for (auto p : to[i]) dist2d[i][p.first] = std::min(dist2d[i][p.first], p.second); } for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { if (dist2d[i][k] == INF) continue; for (int j = 0; j < V; j++) { if (dist2d[k][j] == INF) continue; dist2d[i][j] = std::min(dist2d[i][j], dist2d[i][k] + dist2d[k][j]); } } } } void dump_graphviz(std::string filename = "shortest_path") const { std::ofstream ss(filename + ".DOT"); ss << "digraph{\n"; for (int i = 0; i < V; i++) { for (const auto &e : to[i]) ss << i << "->" << e.first << "[label=" << e.second << "];\n"; } ss << "}\n"; ss.close(); return; } }; int main() { int X, Y, N; cin >> X >> Y >> N; X--, Y--; vector> v2xy(8); constexpr int H = 600; constexpr double inf = 1e50; int sx, sy; REP(i, N) { int x, y, v; cin >> x >> y >> v; x--, y--; if (i == 0) sx = x, sy = y; v2xy[v].emplace_back(x, y); } dbg(v2xy); vector vinv(20), vinv1(20); FOR(d, 1, vinv.size()) vinv[d] = 1000.0 / d; FOR(d, 1, vinv.size()) vinv1[d] = 1.0 / d; dbg(vinv); dbg(vinv1); vector>> D(8, vector(H, vector(H, inf))); FOR(v, 1, 8) { ShortestPath graph(H * H + 1); auto f = [&](int i, int j) { return i * H + j; }; for (auto [x, y] : v2xy[v]) graph.add_edge(H * H, f(x, y), 0); REP(i, H) REP(j, H - 1) graph.add_edge(f(i, j), f(i, j + 1), 1); REP(i, H) REP(j, H - 1) graph.add_edge(f(i, j + 1), f(i, j), 1); REP(i, H - 1) REP(j, H) graph.add_edge(f(i, j), f(i + 1, j), 1); REP(i, H - 1) REP(j, H) graph.add_edge(f(i + 1, j), f(i, j), 1); graph.solve(H * H); REP(i, H) REP(j, H) D[v][i][j] = graph.dist[f(i, j)] * vinv[v]; } vector mat(H, vector(H, inf)); mat[sx][sy] = 0; using P = pair; priority_queue, greater

> pq; pq.emplace(0, pint(sx, sy)); while (pq.size()) { auto [dnow, xy] = pq.top(); pq.pop(); auto [x, y] = xy; if (mat[x][y] < dnow) continue; array dx{1, -1, 0, 0}; array dy{0, 0, 1, -1}; FOR(v, 1, 8) { double tstart = max(D[v][x][y], dnow); if (tstart == inf) continue; REP(d, 4) { int nx = x + dx[d], ny = y + dy[d]; if (nx < 0 or ny < 0 or nx >= H or ny >= H) continue; if (chmin(mat[nx][ny], tstart + vinv[v])) pq.emplace(mat[nx][ny], pint(nx, ny)); FOR(vnxt, v + 1, 8) { if (D[vnxt][nx][ny] < mat[nx][ny] and mat[nx][ny] > mat[x][y]) { dbgif(x == 1 and y == 0 and nx == 2 and v == 1 and vnxt == 2, mat[nx][ny]); dbgif(x == 1 and y == 0 and nx == 2 and v == 1 and vnxt == 2, D[vnxt][nx][ny]); double dt = (1000.0 + (tstart - D[vnxt][nx][ny]) * v) * vinv1[vnxt + v]; if (dt >= 0 and dt * vnxt <= 1000 and chmin(mat[nx][ny], D[vnxt][nx][ny] + dt * 2)) { pq.emplace(mat[nx][ny], pint(nx, ny)); } } } } } } FOR(d, 1, 8) { dbg(d); REP(x, 5) { vector v(10); REP(j, v.size()) v[j] = D[d][x][j]; dbg(v); } } REP(x, 10) { REP(y, 10) cerr << mat[x][y] << ' '; cerr << '\n'; } cout << mat[X][Y] << '\n'; }