Just now wrote a "Sweet Butter", is the classic topic of Usaco. That problem with SPFA how to find all can not, look at others pas easy over all kinds of humble. Huang said the best solution should be the heap optimization of the DIJ, I have not blood, and so on after the study of the butter to write.
Topic:
Title Description
Description On one of the beautiful planets in Galaxy 1. A certain Chen will go to mark the Galaxy of N, from Galaxy G1 to G2, a certain Chen need to spend C1 price [mainly fuel, but also with the grox of the forces along the way to combat the cost],c1 less than 0 is because such a galaxy travel, will bring some benefits [from the logistics difference, And some colonies of taxes .... Accordingly, C2 represents the cost of reaching G1 from the galactic G2. According to some Chen, there are a number of inter-galactic pathways such as M-bar. For combat readiness, he needs to choose a route with the lowest total cost.Enter a description input
Description The input file includes multiple sets of data. For each set of data, the first line has 2 integer n,m, such as the meaning in the title description, 1<=n<=1000,0<=m<=10000. The next M-line, each row has four integer g1,g2,c1,c2, as the meaning of the title description. 0<=g1,g2<=n. Input data guarantees that all integers are within the range of [-10000..10000]. The n=0,m=0 identifies the end of the input data. Each input file contains no more than 10 sets of data.outputs description output
Description For each set of input data, the output line is the cost of a route from Galaxy 1 to the smallest cost of Galaxy N. If such a route does not exist, output ' no such path '.sample input to
sample3 2 1 2 2-1 2 3 0 1 0 0Sample output Sample
outputs2
The description of the title is long and goes to the useless part.
So, on this topic, I am the first time to write an array of analog linked list implementation, for SPFA does improve a lot of efficiency, if coupled with SLF and lll may be more efficient (need to write a priority queue + struct and overload the definition of the comparison function). Negative ring judgment, as long as the addition of an array can be, because the worst case is for this point, all points will change, will relax the point, the most slack n times, is the queue n times. If the number of >n, then there must be a negative ring, has been along the negative loop to the shortest possible short-circuit can be infinitely small, so there is no minimum road.
Put code
This is very important, and we should always look at the study in the future
--a setting sun in the water, Ban Jiang half nancore.
"Daily Learning" "SPFA negative loop + array simulation chain list" codevs2645 Spore