Stones (hdu1896) priority queue

Source: Internet
Author: User

[Topic]

Given n beads numbered from 1 to n, followed by m pairs (x, y), it indicates that beads X and Y can be adjacent and should be made up of the smallest beads, and the number of beads used is an odd number greater than or equal to 3. Calculate the value of the smallest number of beads.

 

[Detailed analysis]

When I first saw the question, I just got a DIY: the first question in the 2008 "sunline Cup" Invitational competition. I thought about it and thought it could be converted into a minimal ring, I will have the smallest ring on my own (For details, refer to my blog on the smallest ring ). The data volume is 1000. I think the Floyd algorithm is okay. Write it. The result is a timeout. After the game, I tried Baidu and read a lot of explanations. The following is a detailed explanation.

 

The complexity of this type of questions is analyzed. You can enumerate each point one by one. For the depth of each enumeration record accessing this point, you only need to add the current depth to the depth of access to this point. calculate the total number of points used and use two queues to form layer-by-layer traversal (ensure that the path is not repeated) it feels good that the minimum value calculated by each enumeration can be used as the next threshold value to narrow down the scale.

 

 

 

# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace STD;
Struct point
{
Int X, Y;
Friend bool operator <(point a, point B)
{
If (A. X! = B. X)
Return A. x> B. X;
Return A. Y> B. Y;
}
};
Int main ()
{
Int m, n, I, J, K, h;
Int ans, sum;
Point cur, in;
Scanf ("% d", & N );
While (n --)
{
Priority_queue <point> q;
Scanf ("% d", & M );
For (I = 0; I <m; I ++)
{
Scanf ("% d", & cur. X, & cur. y );
Q. Push (cur );
}
Ans = 0;
Sum = 0;
While (! Q. Empty ())
{
Sum ++;
Cur = Q. Top ();
Q. Pop ();
If (ANS <cur. X)
Ans = cur. X;
If (sum % 2 = 0)
Continue;
In. x = cur. x + cur. Y;
In. Y = cur. Y;
Q. Push (in );
}
Printf ("% d \ n", ANS );
}
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.