[Bzoj3669] [noi2014] Magic Forest (spfa dynamic queue Addition Algorithm)

Source: Internet
Author: User
3669: [noi2014] Magic Forest time limit: 30 sec memory limit: 512 MB
Submit: 254 solved: 140
[Submit] [Status] Description

In order to get the true story of everyone in calligraphy, Mr. e made up his mind to visit the Hermit Living in the Magic Forest. The magic forest can be regarded as an undirected graph containing m edges of N nodes. The node number is 1. N and the edge number is 1. M. At the beginning, Mr. E was at Node 1, while the hermit lived at node n. John needs to use this magic forest to visit the hermit.

The Magic Forest contains monsters. Whenever someone goes through a side, the monster on the side will launch an attack on it. Fortunately, there are two types of daemon on nodes: A-type daemon and B-type daemon. Small e can use their power to achieve their own goals.

As long as Xiao e carries enough daemon, monsters will not launch attacks. Specifically, each edge ei in an undirected graph contains two weights: AI and Bi. If the number of A-type daemon is not less than that of AI, and the number of B-type daemon is not less than that of Bi, monsters on this side will not launch attacks against people on this side.When and only whenPassThisSliceMagic ForestNoAny sideOfMonstersDirectionEIP attacks,He can succeed.FindHermit.

Because it is very troublesome to carry a daemon, Mr. E wants to know that to successfully visit the hermit, he must carry at least the total number of daemon.DaemonOfTotal numberThe sum of the number of type A daemon and the number of type B daemon.

Input

The row 1st contains two integers, N and M, indicating that the undirected graph has n nodes and m edges. Next, Row M contains four positive integers Xi, Yi, AI, AND Bi, which describe the I-th undirected edge. XI and Yi are the labels of the two ends of the edge. The meanings of AI and Bi are described in the question. Note that the data may contain duplicate edges and auto rings.

Output

Output an integer in a row: if small e can successfully visit the hermit, It outputs the total number of daemon that small e must carry at least. If small e cannot visit the hermit no matter how, output "-1" (without quotation marks ).

Sample input [input Example 1]
4 5
1 2 19 1
2 3 8 12
2 4 12 15
1 3 17 8
3 4 1 17
[Example 2]
3 1
1 2 1 1

Sample output [Output Example 1]
32
[Example 1]
If the small e path is 1 → 2 → 4, 19 + 15 = 34 daemon will be carried;
If small e follows the path 1 → 3 → 4, it must carry 17 + 17 = 34 daemon;
If the small e path is 1 → 2 → 3 → 4, 19 + 17 = 36 daemon will be carried;
If small e follows the path 1 → 3 → 2 → 4, 17 + 15 = 32 daemon genie are required.
To sum up, Xiao e must carry at least 32 daemon.
[Output Example 2]
-1
[Example 2]
Small e cannot reach Node 3 from node 1, so it outputs-1.
Hint

2 <= n <= 50,000

0 <= m <= 100,000

1 <= ai, Bi <= 50,000

Question: the shortest path of two vertices.

Question: The positive solution (which may be slower than the reverse solution) is the linkcut tree, which is the shortest path of the spfa enumerated weight A and B.


Spfa implementation: Obtain the answer by updating ans of enumeration a weights.

Optimization: 1. Do not update the DIST array (monotonically guaranteed) (otherwise mad TLE)

2. Add edges as a weight increases, and enable vertices to join the queue outside the function (otherwise, mad wa)

3. Sort the weights and then enumerate them. (Otherwise, the constant TLE may exist)

The following are optional optimizations:

4. Edge Weight sorting can ensure linear time complexity when edge adding.

5. Optimize the spfa (dijsktra) priority queue (heap optimization ). (Because it is not the shortest path, but the Extreme Value of the edge weight, the optimization effect is not obvious or even slower)

6. Remove the weight of a during Enumeration

7. minimum a weight for the outbound edge of Source Vertex (1) Mina, minimum a weight for the inbound edge of sink vertex (n) minb, then, when enumerating the single-weight spfa of A and running B, you can skip spfa when enumerating the value <max (Mina, minb!

Note: 1. The Dist must be in the memory set before the primary function enumerates the weight. Dist [s] (Dist [1]) = 0;

2. Do not add edge leaks.


# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> # define n 50100 # define M 201000 using namespace STD; struct syndra {int U, V, len, next; bool operator <(const syndra & A) const {return. next> next ;}} EP [m], E [m]; struct Fiona {int F, V; Fiona (int A, int B): F (B ), V (a) {} bool operator <(const Fiona & A) const {return. F <F ;}}; int head [N], CNT, n, m, limit;/* For syndra */INT lsh [m]; /* for discretization */INT Dist [n ], In [N];/* For spfa */void add (int u, int V, int Len) {CNT ++; E [CNT]. V = V; E [CNT]. len = Len; E [CNT]. next = head [u]; head [u] = CNT;} void AD (int u, int V, int Lena, int lenb) {++ CNT; EP [CNT]. U = u; EP [CNT]. V = V; EP [CNT]. next = Lena; EP [CNT]. len = lenb;} priority_queue <Fiona> q; void spfa () {int I, U, V; Fiona x (0, 0); While (! Q. empty () {x = Q. top (); q. pop (); U = x. v; in [u] = 0; for (I = head [u]; I; I = E [I]. next) {v = E [I]. v; If (Dist [v]> MAX (Dist [u], E [I]. len) {Dist [v] = max (Dist [u], E [I]. len); If (! In [v]) {in [v] = 1; q. push (Fiona (v, DIST [v]) ;}}} int main () {// freopen ("Forest. in "," r ", stdin); // freopen (" Forest. ans "," W ", stdout); int I, J, K, num; int A, B, C, D; int ans, Ma, MB; int U, V; scanf ("% d", & N, & M); MA = Mb = 0x3f3f3f; for (I = 1; I <= m; I ++) {scanf ("% d", & A, & B, & C, & D); Ad (A, B, C, D ); lsh [I] = C; if (a = 1 | B = 1) Ma = min (MA, C ); if (A = n | B = N) MB = min (MB, c);} Sort (lsh + 1, lsh + m + 1 ); sort (EP + 1, EP + m + 1); ans = 0x3f3f3f3f; MA = max (MA, MB); memset (Dist, 0x3f, sizeof (DIST )); dist [1] = 0; For (num = CNT = 1, I = 1; I <= m; I ++) {for (; num <= m; num ++) {If (EP [num]. next> lsh [I]) break; u = EP [num]. u; V = EP [num]. v; add (u, v, EP [num]. len); add (v, U, EP [num]. len); q. push (Fiona (u, DIST [u]), in [u] = 1; q. push (Fiona (v, DIST [v]), in [v] = 1;} If (lsh [I] <Ma) continue; if (lsh [I] = lsh [I + 1]) continue; Limit = lsh [I]; spfa (); ans = min (ANS, dist [N] + limit);} If (ANS = 0x3f3f3f3f) printf ("-1"); else printf ("% d \ n", ANS ); return 0 ;}



[Bzoj3669] [noi2014] Magic Forest (spfa dynamic queue Addition Algorithm)

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.