zoukankan      html  css  js  c++  java
  • C

    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 }
  • 相关阅读:
    Vmware14中设置Centos7静态密码
    字符串和集合截取
    通过FTP连上centos7
    centos7 离线安装mysql-5.7.21
    基于jackson注释@JsonFormat 格式化时间少8小时
    Kotlin基础学习笔记 (三)
    Android 常用开源框架源码解析 系列 (零)引言
    Kotlin基础学习笔记 (一)
    Android 常用开源框架源码解析 系列 (十一)picasso 图片框架
    Android 常用开源框架源码解析 系列 (十)Rxjava 异步框架
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7233797.html
Copyright © 2011-2022 走看看