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
  • 相关阅读:
    Oracle 获取表结构信息
    EasyUI layout动态设置Split属性
    jquery easyui-datagrid 如何清空数据
    ORacle 复制表
    aspx调用webmethod
    RDLC添加链接
    Nginx 负载均衡 ip_hash , hash key(consistent) url_hash, least_conn
    Nginx 提升吞吐量利器 Keeplived
    Nginx Gzip 的正确使用
    Nginx 切割日志
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3365447.html
Copyright © 2011-2022 走看看