Poj2531--network saboteur (search exercise 7-dfs or random algorithm)

Source: Internet
Author: User

Network saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9504 Accepted: 4509

Description

A University Network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into and the subnet Works in order to minimize traffic between parts.
A disgruntled computer science student Vasya, after being expelled from the university, decided to has his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between.
Unfortunately, he found, calculating such worst subdivision is one of the those problems he, being a student, failed to so Lve. So he asks-a more successful CS student, to-help him.
The traffic data is given in the form of Matrix C, where Cij is the amount of data sent between and ith jth (nodes = Cji, Cii = 0). The goal is to divide the network nodes into the and the disjointed subsets A and B so as to maximize the SUM∑CIJ (I∈A,J∈B).

Input

The first line of input contains a number of nodes N (2 <= n <= 20). The following n lines, containing n space-separated integers each, represent the traffic matrix C (0 <= Cij <= 10000 ).
Output file must contain a single integer-the maximum traffic between the subnetworks.

Output

Output must contain a single integer-the maximum traffic between the subnetworks.

Sample Input

30 50 3050 0 4030 40 0

Sample Output

90

Source

Find the maximum cut of n points.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; int map[25][25], max1; int state[25], n; void dfs (int u,int sum) {    if (U = = N)    {        max1 = max (max1,sum);        return;    }    DFS (u+1,sum);    State[u] = 1;    int i;    for (i = 0; i < n; i++)    {        if (State[i])        {            sum-= Map[u][i];        }        else            sum + = Map[u][i];    }    DFS (u+1,sum);    State[u] = 0;} int main () {    int i, J;    while (scanf ("%d", &n)! = EOF)    {        memset (state,0,sizeof (state));        for (i = 0; i < n; i++) for            (j = 0; J < N; j + +)                scanf ("%d", &map[i][j]);        max1 = 0;        DFS (0,0);        printf ("%d\n", max1);    }    return 0;}


Look at the online small excellent blog, randomized search ....

#include <iostream>using namespace Std;const int timelimit=2000; The subject time limit is 2000msint main (int i,int j) {int n;while (cin>>n) {/*input*/int w[30][30]={0};for (i=1;i<=n;i++) for (j  =1;j<=n;j++) {cin>>w[i][j];w[j][i]=w[i][j];    Bidirectional full graph}/*random algorithm*/bool subset[30]={false};  Episode A: True B set: Falseint time=timelimit*100;   Make random times as large as possible, random results as close as possible to the optimal solution long max_w=0;  The sum of the weights of the maximal cut and long sum=0;  Current edge cut centralization and while (time--) {int x=rand ()%n+1; Generate random number x, corresponding to a node of the total set X//Note Because the node ordinal used is 1~n, corresponding to the array subscript, the array element labeled 0 is not used//then this must be +1, because if Rand () =n,  Then the n modulo result is 0//At this point will result in the use of the non-existent [0] node, the [n] node should be used is discarded subset[x]=!subset[x]; Change the set position where x is located for (int i=1;i<=n;i++)//Because it is a full graph, each vertex i is associated with X, so all enumerations {if (subset[i]!=subset[x])//node I and X are within two sets sum+=   W[I][X]; That is, because the x is in the set of changes, so that the number of cut edge//Cut set of the original weight to add the current new Cut Edge (i,x) of the weight if (i!=x && subset[i]==subset[x])//Node I   And X are within the same set, but they are not the same element sum-=w[i][x]; That is, because the set of x changes, so that the number of cut edges//cut sets of the original rightvalue to subtract the weight of the currently lost cut Edge (i,x)}if (Max_w < sum) max_w = sum;} Cout<<max_w<<endl;} return 0;}


Poj2531--network saboteur (search exercise 7-dfs or random algorithm)

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.