Editorial for COCI '08 Contest 3 #6 Najkraci
Submitting an official solution before solving the problem yourself is a bannable offence.
A small modification to Dijkstra's shortest path algorithm allows us to find roads that are part of shortest paths from a source city.
For a fixed city , the roads form a directed acyclic graph (DAG) and we can use dynamic programming to calculate:
- , the number of paths from city to city ;
- , the number of paths from city to any other city.
To calculate the values of we use the formulas:
- , for each city such that edge is part of the DAG.
To calculate the values of we use the formula:
- , for each city such that edge is part of the DAG.
The values of are calculated in order in which Dijkstra's algorithm processes the vertices, while is calculated in reverse order.
Knowing these values, we can calculate how many shortest paths from to some other city contains a road .
If road is not part of the DAG, then this amount is zero. Otherwise it is . The total number of shortest paths containing road is calculated as the sum of these products for every starting city .
Comments