zoukankan      html  css  js  c++  java
  • Network Saboteur 简单dfs(), 计算怎样分两个集合 最优问题 用flag[] 数组标记

    Problem 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
    ***************************************************************************************************************************dfs();wuxvjianzhi
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<queue>
     5 #include<cstdio>
     6 using namespace std;
     7 bool flag[1001];
     8 int map[1001][1001];
     9 int maxc,n;
    10 void dfs(int step)
    11 {
    12    if(step==n)//n个数遍历完时,计算结果,与最大值比较
    13     {
    14        int sum=0;
    15       for(int i=1;i<=n;i++)
    16         for(int j=1;j<=n;j++)
    17         {
    18            if(flag[i]&&!flag[j])//分属两个不同的集合
    19             sum+=map[i][j];
    20         }
    21        if(maxc<sum)
    22         maxc=sum;
    23        return;
    24     }
    25     //两种情况都考虑
    26     flag[step]=false;
    27     dfs(step+1);
    28     flag[step]=true;
    29     dfs(step+1);
    30 }
    31 int main()
    32 {
    33     while(scanf("%d",&n)!=EOF)
    34     {
    35       for(int i=1;i<=n;i++)
    36        for(int j=1;j<=n;j++)
    37          scanf("%d",&map[i][j]);
    38       maxc=-1;
    39       memset(flag,false,sizeof(flag));
    40       dfs(1);
    41       printf("%d
    ",maxc);
    42     }
    43     return 0;
    44 
    45 
    46 
    47 }
    View Code
  • 相关阅读:
    通过asp.net 生成xml文件
    listbox 多选处理
    girdview 找到其焦点的笨办法
    关于.net 中调用script的alert后 css失效的办法
    从数据库中读数据中寻找若隐若现的OOP
    Gitlab的安装部署和介绍
    守住你的网站:防御DDoS攻击指南
    分析SQL语句使用资源情况
    Linux下Sniffer程序的实现
    NDIS resources
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3365447.html
Copyright © 2011-2022 走看看