C - Network Saboteur :不熟悉

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

题目意思:有多个结点,每个结点之间有距离,把这些结点分成两部分,A部分结点之间认为没有距离,但与B部分每个结点都有距离,求使其最大的距离

解法:DFS暴力枚举的方法
 1 #include<iostream>
 2
 3 using namespace std;
 4
 5 const int Max = 21;
 6 const bool A = true;
 7 const bool B = false;
 8
 9 int n, map[Max][Max], ans = 0;
10 bool set[Max];
11
12 void dfs(int dep, int sum)
13 {
14     if(dep > n){
15         if(sum > ans)
16             ans = sum;
17         return;
18     }
19
20     int m;
21     m = 0;
22     set[dep] = A;
23     for(int i = 1; i <= dep; i ++)
24         if(set[i] == B)
25             m += map[i][dep];
26     dfs(dep + 1, sum + m);
27
28     m = 0;
29     set[dep] = B;
30     for(int i = 1; i <= dep; i ++)
31         if(set[i] == A)
32             m += map[i][dep];
33     dfs(dep + 1, sum + m);
34 }
35
36 int main()
37 {
38     cin >> n;
39     for(int i = 1; i <= n; i ++)
40         for(int j = 1; j <= n; j ++)
41             cin >> map[i][j];
42     dfs(1, 0);
43     cout << ans << endl;
44     return 0;
45 }
时间: 2024-08-01 19:18:42

C - Network Saboteur :不熟悉的相关文章

poj 2531 Network Saboteur 解题报告

题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最佳的分法就是把点2分为一个子集,另一个子集理所当然就是1.3了. 2-1 的权值是50,2-3的权值是40,那么最大就是50+40 = 90了. 首先dfs的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)

C - Network Saboteur Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divide

poj 2531 Network Saboteur (dfs)

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9364   Accepted: 4417 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

POJ 2531 Network Saboteur(dfs)

题目代号:POJ 2531 题目链接:http://poj.org/problem?id=2531 Language: Default Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13290   Accepted: 6428 Description A university network is composed of N computers. System administrator

poj 2531 -- Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9183   Accepted: 4313 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

POJ2531:Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10978   Accepted: 5290 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully d

【POJ 2531】Network Saboteur

[POJ 2531]Network Saboteur 图的搜索 剪枝真是门学问..剪好了快的可真不是一倍两倍 刚开始搜的思路有问题 TLE了 后来枚举点暴力搜了一发 两百多ms 由于查找时权值是不断增加的 所以直接找集合间最大权的话不方便设置return点 看disscuss发现有一大牛 建了两个数组 通过所有边权-两集合内部边权(去重) 得到答案 dfs的时候找最小内部边权即可 当前状态权值>当前最小内部边权时直接跳出 两个数组分别寸当前状态两个集合中所含的点 每加一个点分别往两边加 假设要将

poj2531(Network Saboteur)

题目地址:Network Saboteur 题目大意: 一个学校的网络由N台计算机组成,为了减少网络节点之间的流量,学校将N个节点分解成两个集合(A.B),student Vasya打算入侵学校的网络,致使集合之间的流量最大,集合之间的流量计算是A集合中的所有节点到B集合中的所有节点的流量之和. 例如 1.2.3 分解集合,A可以为{1}这时B为{2.3}.然后计算 1-2的流量加上 1-3的流量为sum  求出所有可能的sum  取最大的那个. 解题思路: 题意可能有点难理解.  数据很水,直

POJ2531——Network Saboteur(随机化算法水一发)

Network Saboteur DescriptionA 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