結果
問題 |
No.1283 Extra Fee
|
ユーザー |
|
提出日時 | 2020-11-19 18:52:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 3,283 bytes |
コンパイル時間 | 1,258 ms |
コンパイル使用メモリ | 91,956 KB |
最終ジャッジ日時 | 2025-01-16 01:42:56 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/tools/vector_alias.hpp" #include <vector> template <class T> std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); } #line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/dijkstra.hpp" #line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Tools/heap_alias.hpp" #include <queue> template <class T> using MaxHeap = std::priority_queue<T>; template <class T> using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>; #line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp" #line 4 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp" template <class Cost = int> struct Edge { int src, dst; Cost cost; Edge() = default; Edge(int src, int dst, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge<Cost>& e) const { return cost < e.cost; } bool operator>(const Edge<Cost>& e) const { return cost > e.cost; } }; template <class Cost = int> struct Graph : public std::vector<std::vector<Edge<Cost>>> { using std::vector<std::vector<Edge<Cost>>>::vector; void span(bool direct, int src, int dst, Cost cost = 1) { (*this)[src].emplace_back(src, dst, cost); if (!direct) (*this)[dst].emplace_back(dst, src, cost); } }; #line 5 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/dijkstra.hpp" template <class Cost> std::vector<Cost> dijkstra(const Graph<Cost>& graph, int s) { std::vector<Cost> dist(graph.size(), -1); dist[s] = 0; MinHeap<std::pair<Cost, int>> que; que.emplace(0, s); while (!que.empty()) { auto [d, v] = que.top(); que.pop(); if (d > dist[v]) continue; for (const auto& e : graph[v]) { if (dist[e.dst] != -1 && dist[e.dst] <= dist[v] + e.cost) continue; dist[e.dst] = dist[v] + e.cost; que.emplace(dist[e.dst], e.dst); } } return dist; } #line 3 "main.cpp" #include <iostream> #include <algorithm> #line 7 "main.cpp" const std::vector<std::pair<int, int>> dxys{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; using lint = long long; void solve() { int n, m; std::cin >> n >> m; auto xss = vec(n, vec(n, 0LL)); while (m--) { int u, v; std::cin >> u >> v; std::cin >> xss[--u][--v]; } auto enc = [&](int x, int y, int t) { return (x * n + y) * 2 + t; }; Graph<lint> graph(n * n * 2); for (int x = 0; x < n; ++x) { for (int y = 0; y < n; ++y) { for (auto [dx, dy] : dxys) { auto nx = x + dx, ny = y + dy; if (nx < 0 || n <= nx || ny < 0 || n <= ny) continue; for (int t = 0; t <= 1; ++t) { graph.span(true, enc(x, y, t), enc(nx, ny, t), xss[nx][ny] + 1); } graph.span(true, enc(x, y, 0), enc(nx, ny, 1), 1); } } } auto ds = dijkstra(graph, enc(0, 0, 0)); std::cout << ds[enc(n - 1, n - 1, 1)] << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }