結果

問題 No.1283 Extra Fee
ユーザー MisterMister
提出日時 2020-11-19 18:52:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 299 ms / 2,000 ms
コード長 3,283 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 94,856 KB
実行使用メモリ 77,768 KB
最終ジャッジ日時 2024-11-16 07:13:15
合計ジャッジ時間 6,299 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 1 ms
6,820 KB
testcase_09 AC 1 ms
6,820 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 15 ms
7,296 KB
testcase_12 AC 17 ms
7,808 KB
testcase_13 AC 11 ms
6,816 KB
testcase_14 AC 51 ms
16,000 KB
testcase_15 AC 84 ms
23,424 KB
testcase_16 AC 17 ms
7,552 KB
testcase_17 AC 268 ms
70,876 KB
testcase_18 AC 279 ms
70,056 KB
testcase_19 AC 291 ms
73,316 KB
testcase_20 AC 274 ms
68,848 KB
testcase_21 AC 271 ms
69,968 KB
testcase_22 AC 242 ms
62,848 KB
testcase_23 AC 261 ms
75,580 KB
testcase_24 AC 276 ms
75,528 KB
testcase_25 AC 297 ms
75,652 KB
testcase_26 AC 299 ms
75,572 KB
testcase_27 AC 295 ms
75,600 KB
testcase_28 AC 296 ms
75,744 KB
testcase_29 AC 285 ms
77,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0