Topic links: point I ah ╭ (╯ ^ ╰) ╮
Subject to the effect:
points
free edges of the graph, the right side
select comprising a
path points
such that the maximum path and the edge weight
Problem-solving ideas:
Each point can enumerate
, evidently not too
then found a solution to a problem to see something new:Color Coding problems
with
colors for each of the random dot staining
can then be pressed shape
:
expressed in
is the end point, state
largest and the right side
^
^
indicates that if
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
species, there are all the circumstances
species
so the probability of success for the
Expected number of times
We found just need to try
visits All can be successful
time complexity:
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");
}
}