There are N cities and M bidirectional roads. Each road has a particular cost to travel on.
K cities among these have corona testing facility for a particular amount (different cities may have different prices for testing).
Determine for each city, the minimum cost to travel to any corona testing facility and get tested.
EXPLANATION:
Model the problem as a weighted graph with N+1 nodes. Draw an edge between node 0 and node i with a corona testing facility (for all i), and give it weight C_i.
Now, we have reduced the problem to finding the shortest path from each city to node 0. This is equivalent to finding the shortest path from node 0 to each city, which is a classical Dijkstra problem!
That is equivalent though and boils down to adding an imaginary vertix, the same way as multisource BFS. I’d still argue it’s much easier to understand when phrased this way, as it fits into the general idea, so you don’t have manually to skip some steps.
can anybody explain why we need to drow the edge from 0 to ith node (which has corona vaccine)
actually we need to find the shortest path from the ith node + cost of vaccine
please explain so we nebie can also understand
Take it this way. There is a ‘super’ hospital at node 0. All hospitals don’t test the samples by themselves; instead they send the samples to this ‘super’ hospital (which tests it for free).
The distance from hospital i to node 0 is C_i (so the charges you pay for the testing is actually the charges you are paying the hospital to take the samples to hospital 0).
Now, instead of giving the hospital your sample and paying them to send it to hospital 0, why don’t you go yourself to hospital 0 and get your testing done for free? The total cost incurred is the same, in both cases.
That is exactly what the editorial solution does. Let me know if this isn’t clear.
thansk man this make more sence
insted of going to intermediate nearest hospital assume vaccine cost as traviling cost to the ‘super’
hospital , then problem trun’s to the find the shortes path from each node to the super hospital
Or we can also use the vertex with minimum C_i as source because optimal strategy for this vertex will be to stay in the city itself. So we can find shortest path from this city to every other as well.
There is a problem called Water distribution problem , you can look it up , it’s approach is VERY VERY similar to the approach used in this problem
In that problem you need to calculate minimum cost to supply water to all plants , since you need to supply water to all plants what you do is you create a dummy node 0 and apply MST algorithm
But here you need to find minimum cost for each node of the graph , which is similar to to finding shortest path , so u use dijkstra
CONCLUSION : in future if we see a problem with something along the lines of :
some graph is given , centres are given , node to node cost is given (which is positive)
finding total cost to connect everything : dummy node + MST
finding min cost for each node : dummy node + dijkstra
I guess #define int long long will solve the issue, but you can always manually change every int for long long inside your code. It’s because you get possible overflow due to the limits on edge lengths, so you exceed maximum integer value. Also, you shouldn’t set distances to INT_MAX, but to 10^{15} or more, because the longest path could possibly be 2 \cdot 10^5 \cdot 10^9 long.