Poj2531-network saboteur-Violence enumeration + memory/dfs/randomization random mess

Source: Internet
Author: User
Tags cmath

。。。 There is nothing to say about the topic.

Assign n points to two sets, and find the weights of the edges of all points in the sum= a set for each point to the B set.

n=20;

。 The brute force algorithm directly enumerates each case.

1, Violent enumeration complexity 2^19 * 20*20 complexity is slightly larger. TLE ... Add a pruning to remove half of it and run 1S. POJ's data is too water.

Enumeration can be used to use bit operations, re-use of the previous results (complexity 2^n * n), without each enumeration to sum all the values, probably can run 200ms


2, DFS, no pruning can also be 2^20* n ... 400ms, much faster than enumerations (enumerations do a lot of unnecessary additions ...)

Pruning: To seek a-b between the largest external and to the respective minimum of AB and ...

Some Daniel cut the branches directly on the 16MS ...

3. Random mess ...   Every random change 1 nodes from A to B or B to a,for update the sum value, repeat 10W, basically all AC, of course, and the size of the data is related .... Reference to someone else's code is probably also 100+ms



About bitmask. has been traversing every bit of I ... It will be quicker to read someone's writing today ...

Violence enumeration + Memory:

#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm&
Gt   
#include <queue> #include <map> #include <stack> #include <iostream> using namespace std; 
Double eps=0.000001;
int tm[25][25];
int set[25];
int vis[5+ (1&LT;&LT;20)];
int f[5+ (1&LT;&LT;20)];
	int main () {int st,i,j,k;
	int n;
	cin>>n;
		for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {scanf ("%d", &tm[i][j]);
	} int maxx=0;
	
	int all=1<< (n)-1;
		for (k=1;k<all;k++) {if (vis[k]) continue;
		Vis[k]=1; Vis[k^all]=1; Pruning 1/2, so that the complexity becomes 2^ (n-1)//-k&k is the lowbit bit of K (the lowest edge of 1)//k-lowbit (k) to get the "a set" of the last state//then the current state of the sum value f[k]=f[last]+ the current new point
		The sum int lowbit=-k&k; of a set to x before the sum-of the x to the B set
		int last=k-lowbit; 

		F[k]=f[last];    int x=0;

		J is the new Point while (Lowbit) {x++;lowbit>>=1}
			for (j=1;j<=n;j++) {if (1<< (j-1)) &last)//means J already in a set f[k]-=tm[x][j]; else//j in B set F[k]+=tm[x]
		[j]; 
		} F[k^all]=f[k];
	if (F[k]>maxx) maxx=f[k];
	
	printf ("%d\n", Maxx);
return 0; }


Brute Force enumeration code: 1S

#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm&
Gt  
#include <queue> #include <map> #include <stack> #include <iostream> using namespace std;
__int64 inf=15; 
Double eps=0.000001;
int tm[25][25];
int set[25];
int vis[1<<20+5];
	 int main () {int st,i,j,k;
	 int n;
	 cin>>n;
		 for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {scanf ("%d", &tm[i][j]);
	 }} __int64 maxx=0;
	 
	 int all=1<< (n)-1;
		 for (k=0;k<=all;k++) {if (vis[k]) continue;
		 Vis[k]=1; Vis[k^all]=1;
		Pruning 1/2 makes the complexity become 2^ (n-1) set[1]++;
				for (i=1;i<=n;i++) {if (set[i]==2) {set[i]=0;
			set[i+1]++;
		else break;
		} __int64 sum=0; 
					for (i=1;i<=n;i++) {if (!set[i]) continue;
					for (j=1;j<=n;j++) {if (!set[j]) sum+=tm[i][j];
	 } if (Sum>maxx) maxx=sum;
 
printf ("%i64d\n", Maxx);
return 0; }



Dfs:200ms

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
# Include <algorithm>
#include <queue>
#include <map>  
#include <stack>
# Include <iostream>
using namespace std;  
Double eps=0.000001; 
int tmd[25][25];
int set[25];
int Maxx;
	 int n; 
int dfs (int x,int sum)
{ 
	if (x>n)
	{ 
		if (Sum>maxx)
			maxx=sum;
		return 0;
	}
	int tmdp=0,i;set[x]=0;
	for (i=1;i<=x;i++)
	{
		if (!set[i]) continue;
		tmdp+=tmd[i][x];//does not select X into A,sum+=[x to set A and]
	}
	dfs (X+1,SUM+TMDP);
	Tmdp=0;
	Set[x]=1;
	for (i=1;i<=x;i++)
	{
		if (set[i]) continue;
		TMDP+=TMD[I][X];  Select X to A,sum+=[x to B-set and]
	}  
	
 	dfs (X+1,SUM+TMDP);

  
} 

int main ()
{
	int st,i,j,k;
	int t; 
	   cin>>n; 
	 for (i=1;i<=n;i++)
	 {
		 for (j=1;j<=n;j++)
		 {
		 scanf ("%d", &tmd[i][j]); 
		 }
	 maxx=0;
	 DFS (1,0);

	 printf ("%d\n", Maxx); 
return 0;
}


Random mess: http://blog.csdn.net/sssogs/article/details/8221244

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.