Network Saboteur(POJ_2531)

来源:互联网 发布:java并发编程的艺术 编辑:程序博客网 时间:2024/06/02 02:14

Description

A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts.
A disgruntled computer science student Vasya, after being expelled from the university, decided to have his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between two subnetworks.
Unfortunately, he found that calculating such worst subdivision is one of those problems he, being a student, failed to solve. So he asks you, a more successful CS student, to help him.
The traffic data are given in the form of matrix C, where Cij is the amount of data sent between ith and jth nodes (Cij = Cji, Cii = 0). The goal is to divide the network nodes into the two 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

3
0 50 30
50 0 40
30 40 0

Sample Output

90

Source

Northeastern Europe 2002, Far-Eastern Subregion

代码

本来应该用搜索做这题。。结果发现了随机化算法这一好东西,就学了一下。。。真的是好东西。。。虽然感觉这种做法不太可靠。。。上代码。。

随机化算法

#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;int main(){    int n,map[22][22];    while(~scanf("%d",&n))    {        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                scanf("%d",&map[i][j]);            }        }        bool set[22]= {0};        int tim=500000;        int sum=0,maxx=0;        while(tim--)        {            int x=rand()%n+1;            set[x]=!set[x];            for(int i=1; i<=n; i++)            {                if(set[x]!=set[i])                    sum+=map[i][x];                if(i!=x&&set[x]==set[i])                    sum-=map[i][x];            }            if(sum>maxx) maxx=sum;        }        printf("%d\n",maxx);    }    return 0;}

dfs的做法

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,maxx;int map[22][22];bool set[22];void dfs(int x,int sum){    set[x]=!set[x];    for(int i=0;i<n;i++)    {        if(set[x]!=set[i])            sum+=map[i][x];        else if(i!=x)            sum-=map[i][x];    }    if(sum>maxx) maxx=sum;    for(int i=x+1;i<n;i++)    {        dfs(i,sum);        set[i]=0;    }}int main(){    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                scanf("%d",&map[i][j]);            }        }        memset(set,0,sizeof(set));        maxx=0;        dfs(0,0);        printf("%d\n",maxx);    }    return 0;}
0 0
原创粉丝点击