91. Hamilton shortest path

 

 

#include <the iostream> 
#include <CString> 
#include <algorithm> 
the using namespace STD; 

/ * 
	1. Which points are used 
	2. The current stopping point at which 
	
	2 ^ 20 is * = 20 is> 2. 7 * 10 ^ 
	
	F [ state] [J]: 
		state which indicates the current point are used, 
		J represents the last stop point at which 
	
	F [state] [J] = F [state_k] [K] + weight [K] [J] 
	state_k other state represents after the swap set J, 
	state_k to include K 
	
	0,1,4 
	State = 10011 
* / 
const int N = 20 is, M = 20 is <<. 1; 

int n-; 
int F [M] [N], weight [N] [ N]; 

int main () 
{ 
	CIN n->>; 
	for (int I = 0; I <n-; I ++) 
		for (int J = 0; J <n-; J ++) 
			CIN >> weight [I] [J]; 
 
	Memset (F, 0x3F, the sizeof (F));
	F [. 1] [0] = 0;
	
	for(int i = 0; i < 1 << n; ++ i)
		for(int j = 0; j < n; ++ j)
			if(i >> j & 1)	// i的二进制表示里面第j位是不是一
				for(int k = 0; k < n; ++ k)
					if(i - (1 << j) >> k & 1)
						f[i][j] = min(f[i][j], f[i - (1 << j)][k] + weight[k][j]);

	cout << f[(1 << n) - 1][n - 1] << endl;	
	return 0;
} 

  

Guess you like

Origin www.cnblogs.com/mjn1/p/11734068.html