zoukankan      html  css  js  c++  java
  • POJ-2561 Network Saboteur(DFS)

    题目:

    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
    

    题意:

    第一行输入n然后输入n×n的矩阵dis[i][j]代表i到j需要消耗的数值,大致意思是把n个点分成A和B两个集合,求A集合中所有点到B集合中所有点消耗数值的和的最大值。

    分析:

    dfs从1号结点开始搜索,val记录当前情况消耗的数值。可以用vis数组表示两个集合0/1,当把一个元素从0集合移动到1集合时,这个集合的vis从0变为1,它要加上所有初它自己外它到0集合中元素的数值,它要减去所有初它自己外它到1集合中元素的数值(因为改元素原本在0集合中,val中含有它到1元素数值之和,当它变为1元素后要减去)

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 25;
    int dis[maxn][maxn],vis[maxn],ans = 0,n;
    void dfs(int u,int val){
    	vis[u] = 1;
    	int tmp = val;
    	for (int i = 1; i <= n; i++){
    		if (!vis[i]) tmp += dis[u][i];
    		else tmp -= dis[u][i]; 
    	}
    	ans = max(ans,tmp);
    	for (int i = u+1; i <= n; i++){
    		dfs(i,tmp),vis[i] = 0;
    	}
    }
    int main(){
    	while (~scanf("%d",&n)){
    		ans = 0;
    		memset(vis,0,sizeof vis);
    		for (int i = 1; i <= n; i++){
    			for (int j = 1; j <= n; j++){
    				scanf("%d",&dis[i][j]);
    			}
    		}
    		dfs(1,0);
    		printf("%d
    ",ans);	
    	}
    	return 0;
    }
    
  • 相关阅读:
    list接口如何使用
    分页导航jsp
    jstl遍历list的jsp
    sql分页查询
    sql计算总页数
    类和对象,类定义了对象的特征和行为。属性,方法。
    编写一个带有main函数的类,调用上面的汽车类,实例化奔驰、大众、丰田等不同品牌和型号,模拟开车过程:启动、加速、转弯、刹车、息火,实时显示速度。
    java编写一个汽车类,有属性:品牌、型号、排量、速度,有方法:启动、加速、转弯、刹车、息火
    jenkins 集成 pytest + allure
    jenkins环境安装(windows)
  • 原文地址:https://www.cnblogs.com/hznudreamer/p/12375771.html
Copyright © 2011-2022 走看看