Poj 2531 (randomization)

Source: Internet
Author: User

Give a full graph and find the maximum cut.

It is said that this is an NP-hard problem. I started to think about DFS + backtracking, And the brute force search times out. Later, I tried some optimizations based on other users' search results. 400 ms. There may be better pruning methods, but unfortunately I did not find them. Here, I will take a look at the randomization algorithm. After all, I have never seen it before.

Burst search code:
# Include <iostream> <br/> # include <cstring> <br/> using namespace STD; <br/> int G [22] [22]; <br/> int Val [22]; <br/> int max_sum = 0; <br/> int sum = 0; <br/> void DFS (int n, int t) <br/>{< br/> int I, j; <br/> If (t = N) <br/>{< br/> If (sum> max_sum) <br/> max_sum = sum; <br/> return; <br/>}< br/> Val [T] = 0; <br/> int TMP = 0; <br/> for (I = 0; I <t; I ++) <br/>{< br/> If (Val [I] = 1) <br/> TMP + = G [T] [I]; <br/>}< br/> sum + = TMP; <br/> DFS (n, t + 1); <br/> sum-= TMP; <br/> TMP = 0; <br/> Val [T] = 1; <br/> for (I = 0; I <t; I ++) <br/>{< br/> If (Val [I] = 0) <br/> TMP + = G [T] [I]; <br/>}< br/> sum + = TMP; <br/> DFS (n, t + 1); <br/> sum-= TMP; <br/>}< br/> int main () <br/>{< br/> int N; <br/> int I, J; <br/> CIN> N; <br/> for (I = 0; I <n; I ++) <br/> for (j = 0; j <n; j ++) <br/> CIN> G [I] [J]; <br/> DFS (n, 0 ); <br/> cout <max_sum <Endl; <br/>}
 

 

Later I referred to other people's randomization algorithms and thought it was quite classic:

It is to divide a large set S into two sets S1 and S2, then randomly select a point from the set S, change the set where it is located, and then form a new S1 and S2, calculate the sum of the new weights.

In this way, after multiple random selections, we can obtain the approximate optimal solution (usually the optimal solution ).

# Include <iostream> <br/> # include <cstdlib> <br/> # include <cstring> <br/> using namespace STD; <br/> int G [22] [22]; <br/> int Val [22]; <br/> int max_sum = 0; <br/> int sum = 0; <br/> void random (int n) <br/>{< br/> int sum1 = 0; <br/> int sum2 = 0; <br/> int sum = 0; <br/> Int J = 0; <br/> int I = 0; <br/> for (j = 0; j <100000; j ++) <br/>{< br/> int r = rand () % N; <br/> sum1 = 0; <br/> sum2 = 0; <br/> If (Val [R] = 0) <br/> {<br/> Val [R] = 1; <br/> for (I = 0; I <n; I ++) <br/> If (Val [I] = 1) <br/> sum1 + = G [r] [I]; <br/> for (I = 0; I <n; I ++) <br/> If (Val [I] = 0) <br/> sum2 + = G [r] [I]; <br/> // cout <sum1 <"" <sum2 <Endl; </P> <p> sum + = sum2-sum1; <br/> If (sum> max_sum) <br/> max_sum = sum; <br/> // cout <max_sum <Endl; <br/>}< br/> else <br/> {<br/> Val [R] = 0; <br/> for (I = 0; I <N; I ++) <br/> If (Val [I] = 0) <br/> sum1 + = G [r] [I]; <br/> for (I = 0; I <n; I ++) <br/> If (Val [I] = 1) <br/> sum2 + = G [r] [I]; <br/> sum + = sum2-sum1; <br/> If (sum> max_sum) <br/> max_sum = sum; <br/>/* <br/> cout <sum1 <"" <sum2 <Endl; <br/> If (sum1 <sum2) <br/> max_sum + = sum2-sum1; <br/> cout <max_sum <Endl; <br/> */<br/>}< br/> cout <max_sum <Endl; <br/>}< br/> int main () <br/>{< br/> int N; <br/> int I, J; <br/> CIN> N; <br/> for (I = 0; I <n; I ++) <br/> for (j = 0; j <n; j ++) <br/> CIN> G [I] [J]; <br/> memset (Val, 0, sizeof (VAL )); <br/> random (n); <br/>}

Related Keywords:

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.