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
  • 相关阅读:
    80386寄存器
    删除 Windows 旧 OS 加载器
    [C#] Socket 通讯,一个简单的聊天窗口小程序
    [erl] erlang 进程注册和注销
    VB中 '&' 和 '+' 号的区别
    如何成为一个牛逼的程序员
    [VB] if 判断语句 和 If、IIf函数的比较
    C#中通过反射方法获取控件类型和名称
    薪资至少10K的一道题,你能拿下吗
    Jass 技能模型定义(—):半人马酋长的反击光环
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3365447.html
Copyright © 2011-2022 走看看