[Daily learning] [Dijkstra heap optimization] codevs2038 sweet butter problem solution, straight stracodevs2038
Reprinted please indicate the source[Ametake All Rights Reserved]Http://blog.csdn.net/ametake
Put the question first, from USACO
Description
Description
Farmer John found the sweetest way to make butter in all Wisconsin: Sugar. Put sugar on a farm, and he knows that N (1 <= N <= 500) cows will lick it, so that they can make super sweet butter that can sell well. Of course, he will pay extra for the cows.
Farmer John is very embarrassed. He knows that he can train these cows so that they can go to a particular farm when they hear the bell. He planned to put the sugar there and then send out a bell in the afternoon so that he could milk at night.
Farmer John knows that every cow is in his favorite farm (not necessarily a cow in a farm ). Give the farm and farm routes for each ox, and find out the distance and shortest farm for all the cows (he will put sugar there ).
Input description
Input Description
Row 1: Number 3: Number of cows N, number of pastures P (2 <= P <= 800), number of pastures C (1 <= C <= 1450 ).
Line 2 to line N + 1: The farm number where the native cow is located.
Line N + 2 to line N + C + 1: each row has three numbers: A and B, and the distance between the two pastures is (1 <= D <= 255). Of course, the connection is bidirectional.
Output description
Output Description
A row outputs the minimum distance and number of cows that must walk.
Sample Input
Sample Input
3 4 52341 2 11 3 52 3 72 4 3
3 4 5
Sample Image
P2 P1 @--1--@ C1 \ |\ \ | \ 5 7 3 \ | \ \| \ C3 C2 @--5--@ P3 P4
Sample output
Sample Output
8
{Note: The best solution for farm 4 .}
This question took me more than 20 hours, from the earliest SPFA bare, to SLF and LLL optimization, to the poor heap optimization of the dual structure, Dijkstra =. It's all tears, TUT.
According to Zhu's article in NOI, the best method for this question should be SPFA.
But it is clear that codevs has increased the number of data edges, resulting in SPFA degradation to slower than the bare Dijkstra because the time complexity of SPFA is O (km), m is the number of edges, and k is expected to be 2, when the number of edges is large (for example, n * (n-1) and n is the number of points), it is obviously slower than the O (n²) of dij.
As we have summarized before, SPFA is very difficult to deal with dense graphs.
Two optimizations are added. The principle is to put the loose side in front of the queue and implement the queue first. But it still times out. As follows:
As we can see, as the data scale increases, the time increases dramatically because the number of edges increases exponentially.
If the Dij is optimized, the effect will be much better. The principle of heap optimization is to use a minimum heap (c ++ can be implemented through STL priority_queue) to reduce the complexity of "finding the smallest edge of dist greedy" to logn, however, if you use the adjacent table storage to directly find the vertex that can be updated, the maximum complexity of updating dist is n. Therefore, the complexity after heap optimization is O (nlogn), which obviously reduces the time complexity in the dense graph. Compared with SPFA, the space complexity does not increase much (especially when SPFA is added to optimize the storage size, it is also the write struct + priority queue overload ).
Let's take a look at dijkstra optimization time:
It is obvious that there is not much difference between small data and dijkstra needs to be slower. Once the data scale explodes, the advantage of dij is self-evident. Big Data is 10 times faster.
Put the code for these attempts.
This is the most primitive SPFA bare template.
The SPFA can also use SLF and LLL to optimize two optimizations. The essence is the same. You can use struct + heavy load less than number + priority queue implementation. For details, refer to "Marica"
Next, the original version of the dijkstra heap optimization and the final version of the difference is that the elements all join the team at a time each time the smallest element leaves the team and adjust the location of other elements this program WA, but the algorithm itself should be no problem, I don't want to debug it anymore therefore, please note that: this is
Incorrect Program
Next, the final version expands new nodes into the queue every time like bfs
In addition, it is convenient for pascal students to attach the pascal code of Miss Zhu to explain this question. However, please note that the original USACO data, sparse graph, and optimal SPFA are used to explain the situation.
-- A leaf flat boat has a light sail volume and is currently on the southern bank of the chujiang River.