DMOJ · saco08p3

Visiting Grandma

This C++ solution uses shortest paths for DMOJ saco08p3 Visiting Grandma. Read the reasoning, inspect the code, or try your own test case below.

saco08p3MathShortest pathsC++45 lines
Solution233of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Shortest paths

Visiting Grandma matches the distance matrix, cookie-store constraint, shortest route, and route-count modulo 1,000,000.

Math

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

Problem_3_Visiting_Grandma.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int inf = 1e9;
    const long long INF = 1e17; //❄️
    const int mod = 1e6;
    int main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int n; cin >> n;
        vector<vector<pair<int, int>>> adj(n + 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int c; cin >> c;
                if (i == j) continue;
                adj[i + 1].emplace_back(c, j + 1);
            }
        }
        int m; cin >> m;
        unordered_set<int> c;
        for (int i = 0; i < m; i++) {
            int k; cin >> k;
            c.insert(k);
        }
        priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
        pq.push({0, 1, c.count(1) ? 1 : 0});
        vector<array<int, 2>> dist(n + 1, {inf, inf}), cnt(n + 1, {0, 0});
        dist[1][c.count(1) ? 1 : 0] = 0;
        cnt[1][c.count(1) ? 1 : 0] = 1;
        while(!pq.empty()) {
            auto[w, u, s] = pq.top(); pq.pop();
            if (w > dist[u][s]) continue;
            for (auto[wt, v] : adj[u]) {
                int ns = s || (c.count(v) ? 1 : 0);
                if (dist[v][ns] > w + wt) {
                    dist[v][ns] = w + wt;
                    cnt[v][ns] = cnt[u][s];
                    pq.push({dist[v][ns], v, ns});
                } else if (dist[v][ns] == w + wt) {
                    cnt[v][ns] = (cnt[v][ns] + cnt[u][s]) % mod;
                }
            }
        }
        cout << dist[n][1] << " " << cnt[n][1] << '\n';
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.