现在的位置: 首页 > 综合 > 正文

poj2531Network Saboteur(DFS,注要是题目难明)

2018年02月22日 ⁄ 综合 ⁄ 共 1750字 ⁄ 字号 评论关闭

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
题目意思:给出n个点,输入Cij表示从点i到点j的值为Cij.现要要把n个点分成两部分A集合,B集合,求A中的每个点到B中的每个点的总和值最大。
分析:假设有10个点,点i位置为1则在A集合,为0在B集合。
    位置:  1   2   3   4   5   6   7   8   9   10
 第一种:   1   1   1    1   1   1   1   1   1   0
	   1   1   1    1   1   1   1   1   0   1
	   1   1   1    1   1   1    1   1  0   0
	..........................................
最后一种:  0   0    0    0   0   0    0   0   0  0
    
#include<stdio.h>
int a[25],b[25],max,n,map[25][25];
void setmax(int an,int bn)
{
    int i,j,sum=0;
    for(i=1;i<=an;i++)
    for(j=1;j<=bn;j++)
    sum+=map[a[i]][b[j]];
    if(sum>max) max=sum;
}
void dfs(int v,int an,int bn)
{
    if(v==n){setmax(an,bn);return ;}//全部分配好后
    a[an+1]=v+1; dfs(v+1,an+1,bn);
    b[bn+1]=v+1; dfs(v+1,an,bn+1);
}
int main()
{
    while(scanf("%d",&n)>0)
    {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        scanf("%d",&map[i][j]);
        max=0;
        dfs(0,0,0);
        printf("%d\n",max);
    }
}

抱歉!评论已关闭.