Poj 3159 (differential constraint system, spfa + stack, dij + heap)

Source: Internet
Author: User

After having done the first question of the difference constraint system, let's try again. I didn't expect this question to be purely a question about the timing of Short Path Problems. To understand the meaning of the question, I need to pay attention to it. I started with spfa (1), got dis [N], then spfa (N), got dis [1], and got the comparison between the two. In fact, only the former can be used. Dis [N] is the answer.

1. At the beginning, I directly wrote the spfa queue implementation. Needless to say, it must have timed out.

2. I heard that the stack implementation of spfa will be faster, and it will be rewritten to the stack. The result still times out. This is strange. Discuss said that the stack can pass. I am using the standard graph's adjacent table Storage, that is, the Linked List Implementation, But I think other people are using arrays to simulate it. I wonder if array simulation is faster? Again, the tragedy timed out.

3. I also found that my code is a little different from others. I add the edge to add (B, A, K), and people add (a, B, k ), but I think it should have no effect. I changed it to add (a, B, k) and then AC (500 + MS ).

4. In turn, the storage of the adjacent table of my original standard graph times out again. This makes me a little confused. Isn't it true that pointer operations are more efficient? Why can't this be reflected? What's more, the maximum time of this question is 1.5 s, and 0.5 s for Array simulation, and timeout for linked list... I cannot figure it out.

5. in discuss, the question "dij should be used if there are more vertices and edges". The general dij time complexity is (N ^ 2) certainly times out, therefore, we need to use "priority queue" to find the smallest dis [I] value in each loop. We can see that the priority_queue in STL of C ++ is used. the tragedy is that I am C. therefore, you have to write the heap to implement the priority queue. After studying for a long time, I wrote it out. The tragedy is the direct wrong.

6. tangle ..... tangle ...... find bugs .... I am looking... find... finally, I found a C-language compatriot. after analyzing the code, there is indeed a loophole that is hard to find: find the nearest point, that is, the root Q [1] In the small root heap, and delete the change point from the queue, at this time, it must be connected to the new heapadjust (1). (My program has no problem in this process). Then, we can relax the adjacent points to this point. After the relaxation, DIS [] will change, the heap must be updated again (this is my failure )..... fix the bug and AC (600 + MS ). oh... yes...

7. I have contributed more than 30 submit questions...

 

My spfa (stack implementation ):

# Include <stdio. h> # include <stdlib. h ># define INF 100000000 struct edge {int e, V;} edge [150005]; int neg; int node [30005]; int next [150005]; int N; int dis [30005], VST [30005]; int Q [30005]; void add (int s, int e, int v) {edge [neg]. E = E; edge [neg]. V = V; next [neg] = node [s]; node [s] = neg ++;} int relax (int s, int e, int V) {If (DIS [s] + v <dis [e]) {dis [e] = dis [s] + V; return 1;} return 0 ;} /* Use the stack to implement a try */INT spfa (INT S0) {int I, T, P, top; for (I = 1; I <= N; I ++) dis [I] = inf; DIS [S0] = 0; Q [0] = S0; Top = 1; VST [S0] = 1; while (top) {T = Q [-- top]; P = node [T]; while (P! =-1) {If (relax (T, edge [p]. E, edge [p]. v )&&! VST [edge [p]. e]) {q [top ++] = edge [p]. e; VST [edge [p]. e] = 1;} p = next [p];} VST [T] = 0;} return 1;} int main () {int M, K, A, B; memset (node,-1, sizeof (node); scanf ("% d", & N, & M); While (M --) {scanf ("% d", & A, & B, & K); add (a, B, k);} spfa (1 ); printf ("% d/N", DIS [N]);}

My dij + heap:

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define maxv 30005 # define MaxE 150005 # define INF 1000000000 struct edge {int e, V;} edge [MaxE]; int N, neg; int dis [maxv], VST [maxv]; int node [maxv], next [MaxE]; int Q [maxv]; int index [maxv]; void add (int s, int e, int V) {edge [neg]. E = E; edge [neg]. V = V; next [neg] = node [s]; node [s] = neg ++;} void swap (int x, int y) {int T; T = Q [x ]; Q [x] = Q [y]; Q [y] = T; index [Q [x] = x; index [Q [y] = y ;} void heapadjust (INT root) {int L, R, min; L = 2 * root; r = 2 * root + 1; min = root; if (L <= Q [0] & dis [Q [l] <dis [Q [Min]) min = L; if (r <= Q [0] & dis [Q [R] <dis [Q [Min]) min = r; If (Min! = Root) {swap (root, min); heapadjust (min) ;}} void buildheap () {int I; Q [0] = N; for (I = 1; I <= N; I ++) {q [I] = I; index [I] = I;} for (I = n/2; I> = 1; I --) heapadjust (I);}/* Because dis [Q [root] has become smaller, update the heap, move Q [root] Up the branches to the proper position */void Update (INT root) {While (root/2 & dis [Q [root] <dis [Q [root/2]) {swap (root, root/2 ); root = root/2 ;}} int getmin () {int min; min = Q [1]; Q [1] = Q [Q [0]; Q [0] --; inde X [Q [1] = 1; heapadjust (1); Return min;}/* Dijkstra algorithm, first queue implementation */void Dijkstra (INT S0) {int I, k, P; for (I = 1; I <= N; I ++) dis [I] = inf; DIS [S0] = 0; buildheap (); while (Q [0]) {k = getmin (); VST [k] = 1; P = node [k]; while (P! =-1) {If (! VST [edge [p]. e] & dis [k] + edge [p]. v <dis [edge [p]. e]) {dis [edge [p]. e] = dis [k] + edge [p]. v; Update (index [edge [p]. e]);/* The index array is used here. If dis [e] is updated, the heap needs to be updated. In fact, the whole heap does not need to be updated, you only need to make the E point move up along the branches, because dis [e] is smaller. */} p = next [p] ;}} int main () {int M, K, a, B; memset (node,-1, sizeof (node )); scanf ("% d", & N, & M); While (M --) {scanf ("% d", & A, & B, & K); add (a, B, k);} Dijkstra (1); printf ("% d/N", DIS [N]); Return 0 ;}

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.