zoukankan      html  css  js  c++  java
  • Network Saboteur POJ

    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
    DFS
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 int a[22][22];
    11 int t;
    12 int b[22];
    13 int i,j;
    14 int sum1=0;
    15 void dfs(int q,int sum)
    16 {
    17     if(q>t)
    18         {
    19             if(sum>sum1)
    20                 sum1=sum;
    21             return;
    22         }
    23     int m=0;
    24     b[q]=1;
    25     for(i=1;i<=q;i++)
    26     if(b[i]==0)
    27     m+=a[i][q];
    28     dfs(q+1,sum+m);
    29 
    30     b[q]=0;
    31     m=0;
    32     for(i=1;i<=q;i++)
    33     if(b[i]==1)
    34     m+=a[i][q];
    35     dfs(q+1,sum+m);
    36 }
    37 int main()
    38 {
    39     cin>>t;
    40     for(i=1;i<=t;i++)
    41         for(j=1;j<=t;j++)
    42         cin>>a[i][j];
    43         dfs(1,0);
    44         cout<<sum1<<endl;
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    文件操作小练习
    阶段练习1
    copy小练习
    小练习
    str 小列题
    条款50:使用自定义的new以及delete的时机会
    条款49:了解new-handle行为
    简单的说一下:tarits技法就是一种模板元编程,起可以将本来处于运行期的事拉到编译期来做,增加了运行效率。 看以非模板元编程的例子,就是前面的那个例子:
    条款47:请使用traits class表示类型信息
    条款46:需要类型转换的时候请为模板定义非成员函数
  • 原文地址:https://www.cnblogs.com/dulute/p/7272640.html
Copyright © 2011-2022 走看看