CSES - Investigation
Authors: Andrew Wang, Chuyang Wang
Explanation
We can run Dijkstra keeping track of
- the distance:
- the number of ways with the minimum distance:
- the minimum flights with the minimum distance: , and
- the maximum flights with the minimum distance: .
For every node, , we take into consideration all of its neighbors, . If we can reach in a shorter distance than its current minimum, we update the distance and reset , , and .
We also have to take into consideration if we can reach in an equivalent distance. If so, we update:
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int MAXN = 100001;const ll MAX = 0x3f3f3f3f3f3f3f3f;const int MOD = int(1e9) + 7;vector<pair<ll, int>> edge[MAXN];
Java
import java.io.*;import java.util.*;public class Investigation {static final int MOD = 1000000007;public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));PrintWriter out = new PrintWriter(System.out);StringTokenizer st = new StringTokenizer(in.readLine());
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!