点点滴滴”

导航

poj 2531 搜索剪枝

Network Saboteur

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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


 #include <iostream>
 #include <string.h>
 #include <stdio.h>

 using namespace std;
 #define maxx 25
 int map[maxx][maxx];
 int set[maxx];  ///判断点是否在集合里
 int ans;
 int n;

 int dfs(int id,int sum)
 {
     set[id]=1;
     for(int i=1;i<=n;i++)   ///与id这个点不同的点  加入到这个边
     {
         if(set[i]==0)
             sum+=map[id][i];
         else
         sum-=map[id][i];
     }
     if(sum>ans)  ///更新最大值
     {
         ans=sum;
     }
     for(int i=id+1;i<=n;i++)  ///搜之后的   全部的点都搜过   求最大的sum
     {
         dfs(i,sum);
         set[i]=0;  ///回溯
     }
 }

 int main()
 {
     while(scanf("%d",&n)!=EOF)
     {
         for(int i=1;i<=n;i++)
         {
             for(int j=1;j<=n;j++)
             {
                 scanf("%d",&map[i][j]);
             }
         }
         memset(set,0,sizeof(set));
         ans=0;
         dfs(0,0);
         printf("%d\n",ans);
     }
     return 0;
 }

  ~~~~~~~~~~~~

自我认为  根本没有剪枝   全都搜索了一遍

posted on 2014-08-17 11:45  点点滴滴”  阅读(219)  评论(0编辑  收藏  举报