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:
- Wrong answer multiple times: the number of random times is insufficient.
- 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;
}