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
  • 相关阅读:
    思科 ASA 系列防火墙 官方文档下载指南
    Batch批量替换hosts
    OPCDA通信--工作在透明模式下的CISCO ASA 5506-X防火墙配置
    OPC DA通讯 KEP6.4 DCOM 配置脚本
    拖放获取文件信息的bat代码
    禁用UpdateOrchestrator重新启动任务
    SIAMATIC S7-1200 中通过 Modbus RTU 如何读取地址范围 9999 到 65535 的输入字
    提问的智慧 (提问前必读)
    [AHK]输入法状态提示,中文状态提示“中”,英文状态提示“EN”[转]
    Wincc V7.3SE安装截图
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3365447.html
Copyright © 2011-2022 走看看