"Daily Learning" "SPFA negative loop + array simulation chain list" codevs2645 Spore

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.