Network Saboteur (深搜)

本文探讨了一个关于如何划分大学网络以最大化两个子网络间流量的问题。通过对矩阵进行操作,采用深度优先搜索算法来寻找最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Network Saboteur

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 27   Accepted Submission(s) : 11
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).<br>Output file must contain a single integer -- the maximum traffic between the subnetworks.<br>
 

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
 

Source
PKU
 

题意:表示题意太难懂,只是题意看了将近半小时。。。理解能力太差了!!给定n个点,以及它们之间的距离,将这些点分成两个集合,集合之间的距离为点之间的距离之和,求最大的距离。

思路:

首先我们将所有的店标记为0(即所有点放在一个集合里),然后取出一个site标记为1(即将该点放在另一个集合里),这是对于和site在一个集合里的点,我们减去他们两个之间的权值,对于不在一个集合里的点,我们加上他们之间的权值。最后的结果为最大值。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
using namespace std;
int map[40][40];
int tong[40];
int n,res;
void dfs(int site,int sum)
{
    int i;
    tong[site]=1;
    int num=sum;
    for(i=0;i<n;i++){
        if(tong[i]==1)   //与site在一个集合里的点 

            num-=map[site][i];//减去他们之间的权值 

        else
            num+=map[site][i];//不在的加上
    }
    res=max(res,num);//依次循环结束找到的最大值  

    for(i=site+1;i<n;i++)//然后遍历剩下的点 

        {
        if(num>sum){//小剪枝,如果加入了点site后两个集合之间的权值小了,则就不需要遍历这个点。加不加这个剪枝都可以通过
        dfs(i,num);
            tong[i]=0;
         }
    }
}
int main()
{
    int i,j;
    while(cin>>n)
    {
        memset(map,0,sizeof(map));
        memset(tong,0,sizeof(tong));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            cin>>map[i][j];
            res=-1000000;
            dfs(0,0);
          cout<<res<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值