noip2017提高组 宝藏

转载【https://blog.csdn.net/qq_35914587/article/details/78964855】

@HT008_123

题面

虽然大佬没有很详细的讲解,但是代码还是很好懂的!!


代码:

#include
#include
#include
#include
#include

#define loop( i, a, b )	for( int i = a; i <= b; i++ )
#define fill( a ) memset( a, 127/3, sizeof( a ) )
#define idx( x ) 1 << ( x - 1 )

using namespace std;

const int maxn = 15;

int n, m, inf;
int edge[maxn][maxn];
int dis[maxn];
int dp[1< dp[s] + dis[i] * edge[i][j] ) {
						int tmp = dis[j];
						dis[j] = dis[i] + 1;
						dp[s | ( 1 << ( j - 1 ) )] = dp[s] + dis[i] * edge[i][j];
						dfs( s | ( 1 << ( j - 1 ) ) );
						dis[j] = tmp;
					}
}

int main() {
	fill( edge );
	inf = edge[0][0];
	scanf( "%d%d", &n, &m );
	for( int i = 1; i <= m; i++ ) {
		int u, v, c;
		scanf( "%d%d%d", &u, &v, &c );
		edge[u][v] = edge[v][u] = min( edge[v][u], c );
	}
	int ans = inf;
	for( int i = 1; i <= n; i++ ) {
		fill( dis );
		fill( dp );
		dis[i] = 1;
		dp[idx( i )] = 0;
		dfs( idx( i ) );
		ans = min( ans, dp[( 1 << n ) - 1] );
	}
	printf( "%d", ans );
	return 0;
}

你可能感兴趣的:(记忆化搜索,noip,dp)