POJ 2679 Adventurous Driving | SPFA + judgement Negative ring

Source: Internet
Author: User

POJ 2679 Adventurous Driving

Disgusting input disgusting surface ah ... There are still a lot of things to learn about this problem.

Description. To graph: There are two Benquan: 1. Cost, 2. Length. Ask to find the path that s to T cost the least. Be sure to spend the least .... The minimum value of the output path length in the least expensive case: Then the side right can be negative. There is no guarantee that s to T must have a way. At the same time there may be multiple routes between point A to B ...

In short, it is. s cannot go to the T output void,s to the path of T with a negative ring (that is, no minimum) output unbound. The other has a solution to the output minimum cost and minimum path length.

Well, the first thing we can do when we start typing is to keep only one of the smallest sides ...

Then the research point of this problem. There is no negative ring on the St path.

I've probably seen two ways of doing this.

First of all.. If the points on the negative ring can reach the T point. That is, there is this point on the St path and the final result is affected by negative loops.

So the first one is.

1. We SPFA as usual. Then count the number of times to queue. And then the number of the queue is more than the total N of the search can reach the T point. As long as there is any one point can reach T point we will output unbound.

And then SPFA how to break it. We changed the number of times to more than N to more than n + several. This ensures that all the points on the negative ring are enqueued for more than n times. For example, we set the boundary to n + 5.

Then void is directly sentenced to dis[t] for not INF. No negative ring also has the solution on the output.

The second method is the sauce-like child.

2. We reverse all the edges and start searching from the T-point. Dfsbfs Casual. Then sift away points that can't reach the T point. Now the original problem is equivalent to the whole new map to determine the negative ring. Run the SPFA just fine.

Then how to delete the point. I saw two ways to do it. One is the direct erase vector .... "2333333 recommended not to use vector2333333 easy to be stuck 233 adjacency table Dafa Good"

One is the adjacency table. Then it is to determine which points can be reached. Re-build the diagram again. Re -..

Well, actually, I have an idea, but I didn't write the idea. I just casually yy a bit. It is.. DFS/BFS Mark end Why to delete edge???? When you expand the node, you can just get to the point where it's not possible to reach the T-point, and if not, direct continue does not need to join the queue. Why no one wrote on the internet .... Interesting.

Then there are three ways of doing it online. I'm moving in.

1: adjacency table (forward star) Save diagram, use the first method upstairs to solve the problem, and then use DFS every time. The queue for SPFA is optimized with a scrolling queue. 896k. 32ms.

Code

2:vector implementation, DFS tag delete point. 940k. 47ms ... Vector Card constant Ah ...

Code

3. adjacency table (forward star): Then also the search mark Delete point ... Use of BFS. Then I re-created the map again. There is no difference between this method and the second one except the storage method and the search mode.

But look at the time and space complexity of others. 1212k. 0ms.

0ms. 0ms. 0ms. 0ms!!!!!!!!

I think 2 is supposed to be vector slow speed 233 then BFS faster than DFS then so quite space change time 23333

The first one to see efficiency should be without the following two high efficiency. Well......

Code

Oh, my God. For personal reasons, it's not my slag code. Tatat

But in order to prove that I wrote:

Well.. Just the sauce ....

------------------------------------------------------

POJ 2679 Adventurous Driving | SPFA + judgement Negative ring

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.