poj2531

看了一下0ms,16ms,100ms左右过了的代码,思维量对我来说比较大,不是很容易理解。

我的作法:

用并查集算权值和。

用dfs枚举两个点集的所有可能,由于是完全图,枚举一半的点即可。

#include<iostream>
#include<cstring>
using namespace std;
int map[30][30],N,vis[30],MAX;
void getdate()
{
	int i,j;
	scanf("%d",&N);
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
			scanf("%d",&map[i][j]);
}
void dfs(int start,int v2)
{
	int i,j,sum;
	if(v2==0)
	{
		sum=0;
		for(i=0;i<N;i++)
			if(vis[i]==1)
				for(j=0;j<N;j++)
					if(vis[j]==0)
						sum+=map[i][j];
		MAX=max(MAX,sum);
		return;
	}
	for(i=start;i<N&&i+v2<=N;i++)
		if(vis[i]==0)
		{
			vis[i]=1;
			dfs(i+1,v2-1);
			vis[i]=0;
		}
}
void solve()
{
	int i,m;
	getdate();
	m=N/2;
	MAX=-1;
	memset(vis,0,sizeof(vis));
	for(i=1;i<=m;i++)
		dfs(0,i);
	printf("%d\n",MAX);
}
int main()
{
	solve();
}

poj2531,布布扣,bubuko.com

时间: 2024-08-06 22:31:23

poj2531的相关文章

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  取最大的那个. 解题思路: 题意可能有点难理解.  数据很水,直

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

C——Network Saboteur (POJ2531)

题目: 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 sc

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(深搜剪枝)

题意就是把节点分成A.B两组,节点间距C给了,要求解分组的方法,使得∑Cij (i∈A,j∈B)最大. 首先把所有节点都放在一组,然后采用深度优先搜索的方法,对每一个节点都做判断是否应该移到另一组去,判断的依据是移过去和不移过去哪个得到的和值比较大(这里移去B组后的计算方法就是加上该点和在A组中的所有点的间距,和减去原本就在B组中的所有点的间距),如果移过去变小了,那么则不移过去,并剪掉该条支路. DFS的本质就是枚举,一种递归的枚举.比如如果这个程序里面不剪枝的话就可以枚举到所有的分组情况.剪

poj-2531

1 #include"iostream" 2 #include"cstring" 3 using namespace std; 4 #define MAXN 20+5 5 int C[MAXN][MAXN]; 6 int vis[MAXN]; 7 int N; 8 int ans; 9 void dfs(int id,int data){ 10 vis[id]=1; 11 int tmp=data; 12 for(int i=1;i<=N;i++){ 13 i

poj2531——dfs递归枚举+小剪枝

POJ 2531  dfs递归枚举 Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9580   Accepted: 4560 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po