CSES - Investigation

Author: Andrew Wang

Time Complexity: O(ElogV)\mathcal{O}(E\log V)

We can run Dijkstra keeping track of the distance: dist[]dist[], the number of ways with the minimum distance: num[]num[], the minimum flights with the minimum distance: minf[]minf[], and the maximum flights with the minimum distance: maxf[]maxf[]. For every node, vv, we take into consideration all of its neighbors, uu. If we can reach uu in a shorter distance than its current minimum, we update the distance and reset num[u]num[u], minf[u]minf[u], and maxf[u]maxf[u]. We also have to take into consideration if we can reach uu in an equivalent distance. If so, we update:

num[u]=num[v]+num[u]num[u] = num[v] + num[u]
minf[u]=min(minf[u],minf[v]+1)minf[u] = min(minf[u], minf[v] + 1)
maxf[u]=max(maxf[u],maxf[v]+1)maxf[u] = max(maxf[u], maxf[v] + 1)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<ll, int>> edge[100001];
ll dist[100001]; ll num[100001];
int minf[100001];int maxf[100001];
bool v[100001];
ll MAX = 1000000000000000L; ll MOD = 1000000007L;
void djikstra(int s){

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!