Graph Theory37 sections · 1633 units
Open in Course

Handling Large Weights

Overflow and precision concerns

In some problems, edge weights are up to 10610^6 and paths can have 10510^5 edges. Maximum distance = 105×106=101110^5 \times 10^6 = 10^{11}. This exceeds 3232-bit integer range (2×109\approx 2 \times 10^9). Use long long in C++ or 6464-bit integers in other languages.

Also set distdist initially to a value larger than any possible path, like 101810^{18}. Using 10910^9 as "infinity" might not be enough. If you add two distances during relaxation, overflow can silently produce wrong answers.