Multi-field correction eighth HDU 1008 Andy and Maze - random-like pressure DP

Topic links: point I ah ╭ (╯ ^ ╰) ╮

Subject to the effect:

     n n points m m free edges of the graph, the right side
    select comprising a k k path points
    such that the maximum path and the edge weight

Problem-solving ideas:

    Each point can enumerate D F S DFS , evidently not too
    then found a solution to a problem to see something new:Color Coding problems
    with k k colors for each of the random dot staining
    can then be pressed shape D P DP
     d p [ i ] [ s ] dp[i][s] expressed in i i is the end point, state s s largest and the right side
     d p [ in ] [ s ] = m a x ( d p [ in ] [ s ] , d p [ v ] [ s dp[u][s] = max(dp[u][s], dp[v][s ^ c O l [ in ] ] ] ) col[u]] ])
     d p [ v ] [ s ] = m a x ( d p [ v ] [ s ] , d p [ in ] [ s dp[v][s] = max(dp[v][s], dp[u][s ^ c O l [ v ] ] ] ) col [v]]])
    indicates that if ( in , v ) (U, v) a linked side, to ensure that the case where only take one for each color, each update


    The longest path where the answer to this, there is a legitimate case k ! k! species, there are all the circumstances k k k^k species
    so the probability of success for the k ! k k \frac{k!}{k^k} Expected number of times k k k ! \frac{k^k}{k!}
    We found just need to try 200 200 visits All can be successful
    time complexity: O ( k k k ! × 2 k × m ) O(\frac{k^k}{k!} \times 2^k \times m)

Core: Color coding randomly shaped pressure DP +

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
using pii = pair <int,int>;
const int maxn = 1e4 + 5;
int T, n, m, k;
int col[maxn], dp[maxn][100];
struct node{
	int u, v, w;
} e[maxn];

int solve(){
	memset(dp, 128, sizeof(dp));
	for(int i=1; i<=n; i++) dp[i][col[i]] = 0;
	for(int s=1; s<1<<k; s++){
		for(int i=1; i<=m; i++){
			int u = e[i].u, v = e[i].v, w = e[i].w;
			if(s & col[u]) dp[u][s] = max(dp[u][s], dp[v][s ^ col[u]] + w);
			if(s & col[v]) dp[v][s] = max(dp[v][s], dp[u][s ^ col[v]] + w);
		}
	}
	int ret = 0;
	for(int i=1; i<=n; i++) ret = max(ret, dp[i][(1<<k)-1]);
	return ret;
}

int main() {
	srand(time(0));
	scanf("%d", &T);
	while(T--){
		scanf("%d%d%d", &n, &m, &k);
		for(int i=1; i<=m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
		int ans = 0;
		for(int i=0; i<200; i++){
			for(int i=1; i<=n; i++) col[i] = 1 << (rand() % k);
			ans = max(ans, solve());
		}
		if(ans) printf("%d\n", ans);
		else puts("impossible");
	}
}
Published 221 original articles · won praise 220 · views 20000 +

Guess you like

Origin blog.csdn.net/Scar_Halo/article/details/103190097