HDU 2363 inserting ing (two points + enumeration + limit the Shortest Path)

Source: Internet
Author: User

Question:
James walked from home to school to take the test. There were all intersections on the road, and they had their own altitude. James's journey from home to school will inevitably go through different intersections, so he will continue to step on and feel uneasy, which will make him uneasy and affect his test. Therefore, he needs to choose a path with the smallest ups and downs to go to school. The so-called "minimum fluctuation" refers to the minimum difference between the highest altitude point and the lowest altitude point on this road.
The shortest distance must be obtained under the premise of least fluctuation.

Analysis and Summary:
This reminds me of a previous question, HDU1598. However, at that time, it was made using the minimal spanning tree. In addition, the question only needs to output the minimum difference without the need for a short circuit.
In this question, according to the increasing height difference, the number of paths that obviously meet the conditions is also increasing, so we can divide the two points "height difference ".
It is not enough to have a "height difference", because the "ups and downs" is equal to the maximum height minus the minimum height, so we need to enumerate the minimum height (lower limit ), when the maximum height is obtained based on the minimum height + the "height difference" (the upper limit is up), you can limit the maximum short circuit with the low and up conditions.
This question is more than 20 because the side that exceeds the "upper limit" is not ruled out when seeking the shortest path.
Then, I tried the method of least spanning tree. First, I found the "minimum difference", the upper limit and the lower limit, and then the shortest short circuit. However, WA is in the middle of the tangle...


Code:
Binary + enumeration + Shortest Path
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <algorithm>
Using namespace std;
 
Const int INF = 0x7fffffff;
Const int VN = 120;
Const int EN = 10005;
 
Int n;
Int m;
Int size;
Int head [VN];
Int h [VN];
Int order [VN];
Int d [VN];
Int up; // Upper Bound
Int low; // lower bound
Bool inq [VN];
 
Struct Edge {
Int v, next;
Int w;
} E [EN];
 
Void addEdge (int u, int v, int w ){
E [size]. v = v;
E [size]. w = w;
E [size]. next = head [u];
Head [u] = size ++;
}
 
Int SPFA (int src ){
Memset (inq, 0, sizeof (inq ));
For (int I = 0; I <= n; ++ I) d [I] = INF;
D [src] = 0;
If (h [src] <low | h [src]> up) return INF; // return INF directly if the start point does not meet the condition
Queue <int> q;
Q. push (src );
While (! Q. empty ()){
Int u = q. front (); q. pop ();
If (h [u] <low | h [u]> up) continue; // exclude
Inq [u] = false;
For (int e = head [u]; e! =-1; e = E [e]. next) if (h [E [e]. v]> = low & h [E [e]. v] <= up) {// restricted
Int tmp = d [u] + E [e]. w;
If (d [E [e]. v]> tmp ){
D [E [e]. v] = tmp;
If (! Inq [E [e]. v]) {
Inq [E [e]. v] = true;
Q. push (E [e]. v );
}
}
}
}
Return d [n];
}
 
Int main (){
Int T, u, v;
Int len, Min, Max;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n, & m );
Size = 0;
Memset (head,-1, sizeof (head ));
For (int I = 1; I <= n; ++ I ){
Scanf ("% d", & h [I]);
Order [I] = h [I];
If (h [I] <Min) Min = h [I];
If (h [I]> Max) Max = h [I];
}
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & u, & v, & len );
AddEdge (u, v, len );
AddEdge (v, u, len );
}
 
Sort (order + 1, order + 1 + n );
Int left = 0, right = Max-Min + 1, mid;
Int ans, dif = INF, minlen = INF;
While (left <right) {// two points "height difference"
Mid = (left + right)> 1;
Bool flag = false;
For (int I = 1; I <= n; ++ I) {// enumerative minimum altitude
Low = order [I];
Up = order [I] + mid; // obtain the upper limit of the altitude.
Int tmp = SPFA (1 );
If (tmp! = INF ){
Flag = true;
Ans = tmp;
Break;
}
}
If (flag ){
Right = mid;
If (mid <dif ){
Dif = mid;
Minlen = ans;
}
Else if (mid = dif & ans <minlen ){
Minlen = ans;
}
}
Else {
Left = mid + 1;
}
}
Printf ("% d \ n", dif, minlen );
}
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.