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
  • 相关阅读:
    【分享】项目开发容易出现的问题?身为前端/后端你见到过吗?
    标准化API设计的重要性
    【分享】对外API接口安全设计
    【实例】调用数据库自动生成接口代码
    【翻译】API-First是什么概念?有什么商业价值?
    保障接口安全的5种常见方式
    【翻译】使用OpenAPI规范进行安全的API设计
    为什么需要API文档
    利用java的反射,实现工厂创建对象
    Cesium入门8
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3365447.html
Copyright © 2011-2022 走看看