SPFA algorithm and its application and optimization

Source: Internet
Author: User

SPFA algorithm and its application and optimization

By MPs

"Problem Introduction"

Another year of Spring festival, because of sleeping in and lead to rob you, can only take a taxi home, and will undoubtedly consume a lot of money (black ... In order to save money as much as possible, you want to go the shortest way, the road will pass through N cities, and every time you pass through the highway between two cities, will lose CI yuan, assuming that it contains all the price (postage, bridge fee, etc.), you are now in 1th City, hope to reach the city of N, What is the minimum cost, please?

"Input description"

The first line, N,m, indicates that there are N cities, M-Highway

The second line to line m+1, three numbers per line u,v,w, indicates that u city to reach v City cost is w yuan (all have a forward edge)

"Data Range"

For 20% of data, n≤100,m≤1,000

For 50% of data, n≤1,000, m≤1,000

For 100% of data, n≤10,000,m≤10,000

Analysis

The topic is not really difficult, a short-circuit bare template topic

But the data made it difficult for beginners, and it was clear that the person who asked the question meant this:

For 20 points, it's for people who can only Floyd.

For 50 points, it is left to Dijkstra or Bellman_ford.

For 100 points, it is left to Heap+dijkstra or spfa+ (slf/lll)

This article focuses on SPFA and optimization (SLF/LLL)

Reference: http://www.cnblogs.com/pony1993/archive/2012/09/07/2675654.html

The SPFA algorithm is based on the improvement of the Bellman_ford algorithm, and the approximate template for the Bellman_ford algorithm is as follows:

for (I=1->V)

for (J=1->e)

if (U,v (-E and E (u,v) +δu<δv)

Δv=e (u,v) +δu

time complexity O (VE)

For large data situations (such as examples) it will be tle, so we need to optimize

So how to optimize it?

We found that every time there is no need to relax the M-bar, only the edge of the current node should be relaxed, but some nodes out of the topological relationship, so we can use the queue/stack to maintain the topological relationship

In this way our time complexity is reduced to O (VK) generally k<2, but there is no absolute proof and targeted data, so unless forced to adopt SPFA (in fact, there is a compelling situation is because the author wants you to use the SPFA ... Otherwise, as far as possible the use of high-efficiency Dijkstra, really do not get a heap optimization, you know, heap optimization Dijkstra can be more than SPFA (and absolutely stable)

While SPFA is a bit annoying to write with a manual queue, our C + + benefits lie in the fact that STL provides us with an absolutely secure queue, but it takes too long, but we can use many optimizations to compensate

There are 4 kinds of optimization schemes (1,3 can only choose 1 kinds)

(1) SLF

Set the current team head for I, will be the point of the queue J, if Δi≥δj, then direct J into the opponent, or the queue tail, efficiency increase 10%~20%, need data structure double-ended queue

(2) LLL (inefficient, do not repeat here)

(3) Prq

Take a priority queue, each with a small storage distance, efficiency boost 20%~30%, alongside the Dijkstra of the heap optimization

(4) Math (for judging negative weight rings)

Because the value of k is variable, there may be very many, so for the judgement of negative weight ring can only helpless tle, then we can use random numbers to generate a problem under the conditions of the largest and most extreme data, and then continuously reduce n

(Supplementary: To determine whether there is a negative weight ring only to determine the number of times each point is >n)

To the end of the original O (1000*1000) algorithm may be optimized to O (1000*3) algorithm, but this optimization is suitable for RP good or mathematical genius use

Recommended Exercises:

Noip2014d2t2 looking for the road

Usaco Fun Party

Usaco Horse-riding repair fence

Another: The shortest path to learn about the future network flow problem has a great help

SPFA algorithm and its application and optimization

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.