提出 #47485327


ソースコード 拡げる

#include <bits/stdc++.h>
#include <atcoder/modint>

namespace {
using ModInt [[maybe_unused]] = atcoder::modint998244353;
using Num [[maybe_unused]] = long long int;
using Vec [[maybe_unused]] = std::vector<Num>;
using Set [[maybe_unused]] = std::set<Num>;
using Mset [[maybe_unused]] = std::multiset<Num>;
// using Edges [[maybe_unused]] = std::vector<std::vector<Num>>;

template<typename T>
using Q [[maybe_unused]] = std::queue<T>;

template<typename T>
using PQ [[maybe_unused]] = std::priority_queue<T, std::vector<T>, std::greater<T>>;
}

struct Edge {
    Num to {0};
    Num cost {0};
};

using Edges = std::vector<std::vector<Edge>>;
Edges edges;
Num n;
Num m;
Num k;
Num answer;

void visit_dfs(Vec& vs, Num cost) {
    if (vs.size() == static_cast<size_t>(n)) {
        answer = std::min(answer, cost % k);
        return;
    }

    const auto froms = vs;
    for(const auto& from : froms) {
        for(const auto& [next, c] : edges[from]) {
            auto it = std::find(vs.begin(), vs.end(), next);
            if (it != vs.end()) {
                continue;
            }

            vs.push_back(next);
            visit_dfs(vs, cost + c);
            vs.pop_back();
        }
    }
}

void solve(std::istream& is, std::ostream& os) {
    is >> n >> m >> k;
    edges.resize(n);
    answer = k;

    for(Num i{0}; i<m; ++i) {
        Num u, v, w;
        is >> u >> v >> w;
        --u;
        --v;
        edges.at(u).push_back(Edge{v, w});
        edges.at(v).push_back(Edge{u, w});
    }

    Vec vs {0};
    visit_dfs(vs, 0);
    os << answer << "\n";
}

int main(void) {
    solve(std::cin, std::cout);
    return 0;
}

提出情報

提出日時
問題 E - Modulo MST
ユーザ zettsut
言語 C++ 20 (gcc 12.2)
得点 475
コード長 1720 Byte
結果 AC
実行時間 696 ms
メモリ 3660 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 475 / 475
結果
AC × 3
AC × 43
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3472 KiB
00_sample_01.txt AC 1 ms 3472 KiB
00_sample_02.txt AC 696 ms 3656 KiB
01_random_03.txt AC 3 ms 3576 KiB
01_random_04.txt AC 1 ms 3356 KiB
01_random_05.txt AC 8 ms 3372 KiB
01_random_06.txt AC 383 ms 3572 KiB
01_random_07.txt AC 1 ms 3504 KiB
01_random_08.txt AC 1 ms 3456 KiB
01_random_09.txt AC 1 ms 3472 KiB
01_random_10.txt AC 1 ms 3660 KiB
01_random_11.txt AC 1 ms 3660 KiB
01_random_12.txt AC 4 ms 3516 KiB
01_random_13.txt AC 5 ms 3656 KiB
01_random_14.txt AC 37 ms 3472 KiB
01_random_15.txt AC 44 ms 3432 KiB
01_random_16.txt AC 147 ms 3504 KiB
01_random_17.txt AC 390 ms 3476 KiB
01_random_18.txt AC 305 ms 3508 KiB
01_random_19.txt AC 307 ms 3660 KiB
01_random_20.txt AC 7 ms 3468 KiB
01_random_21.txt AC 64 ms 3540 KiB
01_random_22.txt AC 2 ms 3500 KiB
01_random_23.txt AC 1 ms 3580 KiB
01_random_24.txt AC 1 ms 3572 KiB
01_random_25.txt AC 1 ms 3568 KiB
01_random_26.txt AC 1 ms 3512 KiB
01_random_27.txt AC 1 ms 3468 KiB
01_random_28.txt AC 1 ms 3472 KiB
01_random_29.txt AC 1 ms 3476 KiB
01_random_30.txt AC 396 ms 3476 KiB
01_random_31.txt AC 1 ms 3576 KiB
01_random_32.txt AC 2 ms 3572 KiB
01_random_33.txt AC 1 ms 3452 KiB
01_random_34.txt AC 1 ms 3512 KiB
01_random_35.txt AC 1 ms 3444 KiB
01_random_36.txt AC 1 ms 3508 KiB
01_random_37.txt AC 1 ms 3500 KiB
01_random_38.txt AC 1 ms 3576 KiB
01_random_39.txt AC 1 ms 3504 KiB
01_random_40.txt AC 189 ms 3580 KiB
01_random_41.txt AC 1 ms 3468 KiB
01_random_42.txt AC 1 ms 3508 KiB