Poj 2531 network saboteur solution report (randomization algorithm)

Source: Internet
Author: User

Source: poj 2531 network saboteur

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2531

 

Solution Type: randomizationAlgorithm

 

Author: Liu yaning

 

Question:

Divide a complete graph into two parts to maximize the Edge Weight of the two parts.

 

Solution:

Randomly change the position of a point, calculate the weight, repeat 200000 times, and take the maximum value output.

 

Submission:

    1. Wrong answer multiple times: the number of random times is insufficient.
    2. Time limit exceeded multiple times. Because the positions of all vertices are randomly allocated each time, the time-out will occur when the number of random times is too large.

 

Note:

This method is usually used. It can be used on poj and zoj ......

This question can also be done using dynamic planning or clever application pruning, but I have not found a solution ......

 

SourceProgram:

# Include <iostream>

 

Using namespace STD;

 

Int map [30] [30], part [30]; // The part array is used to separate points.

 

Int main ()

{

Long N, I, j,;

While (CIN> N)

{

For (I = 1; I <= N; I ++)

For (j = 1; j <= N; j ++)

Cin> map [I] [J];

Memset (part, 0, sizeof (part ));

Long max =-1, t, sum = 0;

T = 200000; // random number of times

While (t --)

{

A = rand () % N + 1; // randomly generate a number and obtain the location of the corresponding vertex.

If (part [a]) part [a] = 0; // change the position of this Vertex

Else part [a] = 1;

For (I = 1; I <= N; I ++) // The right to update the changed Graph

{

If (part [I] & part [a] &! = I) sum-= map [a] [I];

If (! Part [I] &! Part [a] &! = I) sum-= map [a] [I];

If (part [I] &! Part [a]) sum + = map [a] [I];

If (! Part [I] & part [a]) sum + = map [a] [I];

}

If (sum> MAX) max = sum; // update the maximum value

}

Cout <max <Endl;

}

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.