Hamilton ACwing91 shortest path shaped pressure dp

URL: https://www.acwing.com/problem/content/93/

answer:

After the pressure-like violence enumeration updates. $ Dp [i] [j] $ $ I $ denotes a binary number in the position 1 is the position of a point can pass, $ j $ is the current point. The transfer equation is $ dp [i] [j] = min (dp [i] [j], dp [i \ oplus (1 << j)] [k] + dis [k] [j]) $, where $ i \ oplus (1 << j) $ $ I $ is the binary state of $ J $ remove edges, i.e. from a state not directly connected $ $ J from side to $ k $ $ $ J relaxation, and finally determine what side have only enumerate just fine. If you do not like the pressure, it will $ TLE $.

AC Code:

#include <bits/stdc++.h>
using namespace std;
int mp[20][20];
int f[1 << 20][20];
int solve(int n)
{
	memset(f, 0x3f3f3f3f, sizeof(f));
	f[1][0] = 0;
	for (int i = 1; i < (1 << n); ++i)
		for (int j = 0; j < n; ++j)
			if ((i >> j) & 1)
				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] + mp[k][j]);
	return f[(1 << n) - 1][n - 1];
}
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
			scanf("%d", &mp[i][j]);
	printf("%d\n", solve(n));
	return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/Aya-Uchida/p/11470582.html