結果

問題 No.1283 Extra Fee
ユーザー MisterMister
提出日時 2020-11-19 18:52:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 3,283 bytes
コンパイル時間 1,022 ms
コンパイル使用メモリ 94,296 KB
実行使用メモリ 77,392 KB
最終ジャッジ日時 2023-08-10 06:24:35
合計ジャッジ時間 6,405 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 15 ms
7,164 KB
testcase_12 AC 16 ms
7,848 KB
testcase_13 AC 11 ms
6,088 KB
testcase_14 AC 52 ms
16,096 KB
testcase_15 AC 82 ms
23,500 KB
testcase_16 AC 17 ms
7,480 KB
testcase_17 AC 267 ms
70,756 KB
testcase_18 AC 267 ms
70,272 KB
testcase_19 AC 297 ms
73,312 KB
testcase_20 AC 274 ms
68,760 KB
testcase_21 AC 267 ms
69,868 KB
testcase_22 AC 244 ms
62,768 KB
testcase_23 AC 260 ms
75,528 KB
testcase_24 AC 273 ms
75,572 KB
testcase_25 AC 291 ms
75,524 KB
testcase_26 AC 292 ms
75,524 KB
testcase_27 AC 302 ms
75,580 KB
testcase_28 AC 297 ms
75,672 KB
testcase_29 AC 295 ms
77,392 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