poj2531

来源:互联网 发布:超级优化女主角孙菲菲 编辑:程序博客网 时间:2024/06/05 19:30

Network Saboteur
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11265 Accepted: 5432

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

30 50 3050 0 4030 40 0

Sample Output

90

思路:1、把这两个集合标记为0和1,先默认所有点都在集合0里。

            2、依次枚举每个点id,把每个点都放到集合1里去,这个时候就要调整集合的权值了,原来和id都在集合0里的点,要把权值加上;而在集合1里的点,要把权值减去。

            3、权值调整完毕后,和ans比较,如果比ans要大, 调整ans。

            4、如果放到集合1中,调整节点后的权值比放在集合0中要大,那么就默认这个点在集合1中,继续枚举下面的点进行DFS。最终是可以把最有状态都枚举出来的。

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<math.h>int map[21][21];int ans;int set[42];int n;void dfs(int num,int sum){    set[num]=1;    int i;    int data=sum;    for(i=1;i<=n;i++){        if(set[i]){            data-=map[num][i];        }        else{            data+=map[num][i];        }    }    //printf("%d %d\n",num,data);    if(ans<data){        ans=data;    }    for(i=num+1;i<=n;i++){        if(data>sum){            dfs(i,data);            set[i]=0;        }    }}int main(){    while(~scanf("%d",&n)){        int i,j;        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                scanf("%d",&map[i][j]);            }        }        memset(set,0,sizeof(set));        dfs(1,0);        printf("%d\n",ans);    }    return 0;}





0 0