結果

問題 No.845 最長の切符
ユーザー MisterMister
提出日時 2020-08-05 13:34:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 326 ms / 3,000 ms
コード長 1,956 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 81,440 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2023-10-14 14:17:06
合計ジャッジ時間 3,608 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 14 ms
4,348 KB
testcase_16 AC 326 ms
7,532 KB
testcase_17 AC 79 ms
4,348 KB
testcase_18 AC 51 ms
5,108 KB
testcase_19 AC 14 ms
4,352 KB
testcase_20 AC 75 ms
7,532 KB
testcase_21 AC 40 ms
7,552 KB
testcase_22 AC 68 ms
4,348 KB
testcase_23 AC 15 ms
4,348 KB
testcase_24 AC 269 ms
7,532 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 6 ms
7,524 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 5 ms
7,528 KB
testcase_29 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

void solve() {
    int n, m;
    std::cin >> n >> m;

    Graph<int> graph(n);
    while (m--) {
        int u, v, c;
        std::cin >> u >> v >> c;
        graph.span(false, --u, --v, c);
    }

    auto dp = vec(n, vec(1 << n, -1));
    for (int v = 0; v < n; ++v) {
        dp[v][1 << v] = 0;
    }

    int ans = 0;
    for (int b = 0; b < (1 << n); ++b) {
        for (int v = 0; v < n; ++v) {
            if (dp[v][b] == -1) continue;

            ans = std::max(ans, dp[v][b]);

            for (auto e : graph[v]) {
                int u = e.dst;
                if ((b >> u) & 1) continue;

                int nd = dp[v][b] + e.cost;
                int nb = b | (1 << u);
                dp[u][nb] = std::max(dp[u][nb], nd);
            }
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0