2019 Multi-University Training Contest 8 :Andy and Maze 1008(color coding + 状压dp)

Andy and Maze

Time Limit: 15000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 566    Accepted Submission(s): 195

Problem Description

Andy is a famous explorer at Nanjing University second to none. One day he was trapped in a maze. The maze consisted of several rooms, and there was a precious gem in each room. There were also some bidirectional roads connecting some pairs of these rooms. It took some time to travel along the road in either direction.

Andy was in a room, but he didn't know which room he was in. Suddenly, he heard a low voice, saying that, "if you want to get out of the maze, you must collect kgems." Andy wanted to know, what was the maximum possible time to get out of the maze, or it was impossible at all? Note that he couldn't visit the same room more than once.

Input

The first line of input consists of a single integer T (1≤T≤35), denoting the number of test cases. Each test case starts with a line of three integers n,m,k (2≤n≤104,1≤m≤104,2≤k≤6), denoting the number of rooms and the number of roads in the maze, and the number of gems he needed to collect, respectively. Each of the next m lines contains three intergers u,v,t (1≤u,v≤n,u≠v,1≤t≤108), specifying a road connecting the uth and the vth rooms, which took t minutes to travel in either direction. No two roads connected the same pair of rooms.
There are at most 5 test cases with max{n,m}>100.

 Output

For each test case, print the maximum possible time in minute to get out of the maze in one line. If it was impossible to get out of the maze, print impossible instead.

Sample Input

2 4 4 3 1 2 6 2 3 1 2 4 4 1 4 5 5 3 4 1 2 2 2 3 3 4 5 5

Sample Output

11 impossible

 Source

2019 Multi-University Training Contest 8

题意:你迷失在了一个迷宫(图)里,每个房间有一颗宝石,并且从一个房间到达另一个房间(前提是有路)的要耗费一定的时间,现在你并不知道自己在哪个房间里,但是你必须找到k颗宝石才能走出走出迷宫,不能重复经过同一个房间,问收集到k颗宝石你需要最大可能的时间是多少?

题解:color coding 一个看脸的算法,过不了就多交两次,再过不了就把随机次数改大一点再交两次(该题300次足以)。

color coding的思想:

将图上的顶点随机染色,颜色的种类是需要走的路径的长度,在该题颜色种类就是k,然后把颜色状压一下跑一个dp。

dp[i][j] 表示:以 i 点为终点且经过的路径的所有颜色状态为 j 的最大时间。

如果存在一条边 (u, v) ,那么dp转移方程就是:

if( j & (1 << col[v]) ) dp[v][j] = max(dp[v][j], dp[u][j ^ (1<

如果我们需要的那条最大路径上的点恰好染成了两两不相同的颜色,那么最终的答案就是某个 dp[i][(1<<k)-1] (有点意思)

但是如果没有染成不同的颜色呢?是的,那就要看运气了,所以我们要对该图多次染色,每染一次就跑一次dp,只要次数多,总会染成不同的颜色是叭(卧槽)

算一下吧,最大路径上的点恰好染成了两两不相同的颜色的概率是\frac{k!}{k^{k}},所以存在一次染成不同颜色的期望次数是\frac{k^{k}}{k!},因为题中k很小,数据也不多,所以300次随便跑,过不了的话那只能怪自己脸太黑了。

//#include"bits/stdc++.h"
//#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 1e6 + 3;
const int maxn = 1e4 + 5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
const double eps=1e-7;

struct dd { int u, v, w;  } e[maxn];
const int G = 300;
int col[maxn], dp[maxn][1 << 6], n, m, k;

void get_col() { for(int i=1; i<=n; i++) col[i] = rand() % k; }

int get_ans() {
    int S = (1 << k) - 1;
    for(int i=1; i<=n; i++) {
        for(int j=0; j<=S; j++) dp[i][j] = -INF;
        dp[i][1<> T;
    while( T -- ) {
        cin >> n >> m >> k;
        for(int i=0; i

 

你可能感兴趣的:(color,coding,状压dp)