POJ-2531 network saboteur enumeration | randomization

Source: Internet
Author: User

Given a complete graph, we need to divide the graph into two parts and calculate the distance and maximum value of the points after the points of the two parts are cartesian products.

Solution 1: since a given number of points can be at most 20, you can simply enumerate the time complexity of 2 ^ N * O (n.

Solution 2: Use the randomization algorithm to enumerate a vertex and roll the set of the vertex.

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <cstdio> # include <iostream> using namespace STD; int N, G [25] [25]; int Cal (int x) {int A [25], B [25], CA = 0, CB = 0, ret = 0; For (INT I = 0; I <n; ++ I) {If (X & (1 <I) {A [Ca ++] = I + 1 ;} else {B [CB ++] = I + 1 ;}} for (INT I = 0; I <CA; ++ I) {for (Int J = 0; j <CB; ++ J) {RET + = G [A [I] [B [J];} return ret;} int main () {int ret; while (scanf ("% d", & n) = 1) {ret = 0; For (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) {scanf ("% d", & G [I] [J]) ;}} // store the specified information. Int mask = (1 <n)-1; for (INT I = 1; I <mask; ++ I) {ret = max (Ret, Cal (I);} printf ("% d \ n", RET);} return 0 ;}

The following randomization Code G ++ cannot be used. I don't know why.

# Include <cstdlib> # include <cstdio> # include <cstring> # include <iostream> # include <ctime> # include <map> using namespace STD; // The problem is relatively small. // The randomization algorithm can be used to solve the problem. Int N, G [30] [30]; int randtime = 200000; int A [30]; void moveatob (int x, Int & T) {A [x] = 0; For (INT I = 1; I <= N; ++ I) {if (a [I]) T + = G [x] [I]; else t-= G [x] [I] ;}} void movebtoa (INT X, int & T) {A [x] = 1; for (INT I = 1; I <= N; ++ I) {If (! A [I]) T + = G [x] [I]; else t-= G [x] [I] ;}} int randpro () {int Rd, ret = 0, T = 0; For (INT I = 0; I <randtime; ++ I) {RD = rand () % N + 1; // get a vertex, this point is indicated by if (a [RD]) {// description in the set moveatob (Rd, T);} else {// description in the B set movebtoa (Rd, t);} ret = max (Ret, T);} return ret;} int main () {srand (Time (null); While (scanf ("% d ", & n) = 1) {for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) {scanf ("% d", & G [I] [J]) ;}} for (INT I = 1; I <= N; ++ I) {A [I] = 1; // first assume that all elements belong to the first set} printf ("% d \ n", randpro ();} 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.