Submission #24513471


Source Code Expand

#include <bits/stdc++.h>

#define rep(i, n) for (auto i = 0; i < n; ++i)
#define ALL(a) a.begin(), a.end()

using namespace std;

using ll = long long int;
using Graph = vector<vector<int>>;

const int MOD_NUM = 1e9 + 7;

template <class T>
vector<T> input_vec(int N) {
    vector<T> result(N);
    rep(i, N) cin >> result[i];
    return result;
}

int main() {
    // Input
    int N, M;
    cin >> N >> M;
    vector<int> A(M), B(M);
    rep(i, M) cin >> A[i] >> B[i];

    // Process
    Graph graph(N);
    rep(i, M) {
        graph[A[i] - 1].emplace_back(B[i] - 1);
        graph[B[i] - 1].emplace_back(A[i] - 1);
    }

    vector<int> costs(N, N);
    vector<ll> counts(N, 0);
    costs[0] = 0;
    counts[0] = 1;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int current = que.front();
        que.pop();

        int current_cost = costs[current] + 1;
        for (int next : graph[current]) {
            if (current_cost > costs[next]) continue;

            if (current_cost < costs[next]) {
                costs[next] = current_cost;
                counts[next] = 0;
                que.push(next);
            }

            counts[next] += counts[current];
            counts[next] %= MOD_NUM;
        }
    }

    ll ans = counts[N - 1];

    // Output
    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task D - Number of Shortest paths
User Koreander
Language C++ (GCC 9.2.1)
Score 400
Code Size 1404 Byte
Status AC
Exec Time 149 ms
Memory 17848 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 30
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
Case Name Status Exec Time Memory
random_01.txt AC 140 ms 17428 KiB
random_02.txt AC 125 ms 13492 KiB
random_03.txt AC 87 ms 15048 KiB
random_04.txt AC 71 ms 7512 KiB
random_05.txt AC 141 ms 17260 KiB
random_06.txt AC 117 ms 12500 KiB
random_07.txt AC 42 ms 12344 KiB
random_08.txt AC 30 ms 6800 KiB
random_09.txt AC 140 ms 17296 KiB
random_10.txt AC 120 ms 12400 KiB
random_11.txt AC 44 ms 12612 KiB
random_12.txt AC 13 ms 6692 KiB
random_13.txt AC 139 ms 17516 KiB
random_14.txt AC 133 ms 16412 KiB
random_15.txt AC 117 ms 16724 KiB
random_16.txt AC 63 ms 10560 KiB
random_17.txt AC 144 ms 17304 KiB
random_18.txt AC 114 ms 11048 KiB
random_19.txt AC 130 ms 16828 KiB
random_20.txt AC 57 ms 8308 KiB
random_31.txt AC 72 ms 7620 KiB
random_32.txt AC 105 ms 12516 KiB
random_33.txt AC 149 ms 17848 KiB
random_34.txt AC 118 ms 11444 KiB
random_35.txt AC 112 ms 11348 KiB
random_36.txt AC 129 ms 14784 KiB
sample_01.txt AC 2 ms 3496 KiB
sample_02.txt AC 2 ms 3600 KiB
sample_03.txt AC 2 ms 3504 KiB
sample_04.txt AC 1 ms 3424 KiB