poj2531--Network Saboteur(搜索练习7-dfs或随机算法)

Network Saboteur

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9504   Accepted: 4509

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

Source

找出n个点的最大割。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
int Map[25][25] , max1 ;
int state[25] , n ;
void dfs(int u,int sum)
{
    if( u == n )
    {
        max1 = max(max1,sum) ;
        return ;
    }
    dfs(u+1,sum) ;
    state[u] = 1 ;
    int i ;
    for(i = 0 ; i < n ; i++)
    {
        if( state[i] )
        {
            sum -= Map[u][i] ;
        }
        else
            sum += Map[u][i] ;
    }
    dfs(u+1,sum) ;
    state[u] = 0 ;
}
int main()
{
    int i , j ;
    while( scanf("%d", &n) != EOF )
    {
        memset(state,0,sizeof(state)) ;
        for(i = 0 ; i < n ; i++)
            for(j = 0 ; j < n ; j++)
                scanf("%d", &Map[i][j]) ;
        max1 = 0 ;
        dfs(0,0) ;
        printf("%d\n", max1) ;
    }
    return 0;
}

看的网上小优的博客,随机化搜索。。。。

#include<iostream>
using namespace std;

const int TimeLimit=2000;  //本题时间限制为2000ms

int main(int i,int j)
{
	int n;
	while(cin>>n)
	{
		/*Input*/

		int w[30][30]={0};
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				cin>>w[i][j];
				w[j][i]=w[i][j];  //双向完全图
			}

		/*Random Algorithm*/

		bool subset[30]={false};    //A集:true  B集:false
		int time=TimeLimit*100;  //使随机次数尽可能大,随机结果尽可能接近最优解
		long max_w=0;   //最大割的权值之和
		long sum=0;  //当前边割集权和

		while(time--)
		{
			int x=rand()%n+1;  //生成随机数 x,对应于总集合的某个结点x
			                   //注意由于使用的结点序号为1~n,对应了数组下标,下标为0的数组元素没有使用
			                   //那么这里必须+1,因为若rand()=n,那么再对n取模结果就为0
			                   //这时就会导致使用了不存在的 [0]结点,本应使用的 [n]结点就被丢弃了

			subset[x]=!subset[x];  //改变x所在的集合位置

			for(int i=1;i<=n;i++)   //由于是完全图,所以每个顶点i都与x相关联,因此要全部枚举
			{
				if(subset[i]!=subset[x])   //结点i 和 x分别在两个集合内
					sum+=w[i][x];   //就是说因为x所在集合的改变,使得割边的个数增加
				                    //割集的原权值 要加上 当前新加入的割边(i,x)的权值

				if(i!=x && subset[i]==subset[x])  //结点i 和 x分别在相同的集合内,但他们不是同一元素
					sum-=w[i][x];   //就是说因为x所在集合的改变,使得割边的个数减少
				                    //割集的原权值 要减去 当前失去的割边(i,x)的权值
			}

			if(max_w < sum)
				max_w = sum;
		}

		cout<<max_w<<endl;
	}
	return 0;
}
时间: 2025-01-06 02:02:46

poj2531--Network Saboteur(搜索练习7-dfs或随机算法)的相关文章

POJ2531:Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10978   Accepted: 5290 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully d

POJ2531——Network Saboteur(随机化算法水一发)

Network Saboteur DescriptionA 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

poj2531(Network Saboteur)

题目地址:Network Saboteur 题目大意: 一个学校的网络由N台计算机组成,为了减少网络节点之间的流量,学校将N个节点分解成两个集合(A.B),student Vasya打算入侵学校的网络,致使集合之间的流量最大,集合之间的流量计算是A集合中的所有节点到B集合中的所有节点的流量之和. 例如 1.2.3 分解集合,A可以为{1}这时B为{2.3}.然后计算 1-2的流量加上 1-3的流量为sum  求出所有可能的sum  取最大的那个. 解题思路: 题意可能有点难理解.  数据很水,直

poj 2531 Network Saboteur (dfs)

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9364   Accepted: 4417 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

POJ 2531 Network Saboteur(dfs)

题目代号:POJ 2531 题目链接:http://poj.org/problem?id=2531 Language: Default Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13290   Accepted: 6428 Description A university network is composed of N computers. System administrator

【POJ 2531】Network Saboteur

[POJ 2531]Network Saboteur 图的搜索 剪枝真是门学问..剪好了快的可真不是一倍两倍 刚开始搜的思路有问题 TLE了 后来枚举点暴力搜了一发 两百多ms 由于查找时权值是不断增加的 所以直接找集合间最大权的话不方便设置return点 看disscuss发现有一大牛 建了两个数组 通过所有边权-两集合内部边权(去重) 得到答案 dfs的时候找最小内部边权即可 当前状态权值>当前最小内部边权时直接跳出 两个数组分别寸当前状态两个集合中所含的点 每加一个点分别往两边加 假设要将

poj 2531 Network Saboteur 解题报告

题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最佳的分法就是把点2分为一个子集,另一个子集理所当然就是1.3了. 2-1 的权值是50,2-3的权值是40,那么最大就是50+40 = 90了. 首先dfs的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

poj 2531 -- Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9183   Accepted: 4313 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

POJ 2351 Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14087   Accepted: 6893 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully d