LightOJ - 1287 - Where to Run (状压期望dp)

1287 - Where to Run

   PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

Last night you robbed a bank but couldn't escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of some junctions which are connected by some bidirectional roads.

Since police is behind, you have nothing to do but to run. You don't know whether you would get caught or not, but if it is so, you want to run as long as you can. But the major problem is that if you leave a junction, next time you can't come to this junction, because a group of police wait there for you as soon as you left it, while some other keep chasing you.

That's why you have made a plan to fool the police as longer time as possible. The plan is, from your current junction, you first find the number of junctions which are safe (no police are there) and if you go to one of them; you are still able to visit all the safe junctions (in any order) maintaining the above restrictions. You named them 'Elected Junction' or EJ. If there is no such junction; you stop running, because you lose your mind thinking what to do, and the police catch you immediately.

But if there is at least one EJ, you can either fool around the police by staying in the current junction for 5 minutes (actually you just hide there, so the police lose your track thinking which road you might have taken), or you can choose to go to any EJ. The probability of choosing to stay in the current junction or to go to each of the EJ is equal. For example, from the current junction you can go to three EJs, that means the probability of staying in the current junction is 1/4or the probability to go to any of the EJ is 1/4 since you have four options (either stay in the current junction or go to any of the three junctions).

You can fool the police (by hiding) multiple times in a city, but of course the above conditions should be satisfied. And you have decided not to stop in the middle of any road, because you have the fear that, if you stop in the middle of any road, then the police would surround you from both ends.

Now, given the map of the city and the required time for you to travel in each road of the map; you have to find the expected time for the police to catch you.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. Next line contains two integers n (1 ≤ n ≤ 15) denoting the number of junctions and m, denoting the number of roads in the city. The junctions are numbered from 0 to n - 1.

Each of the next m lines contains three integers u v w (0 ≤ u, v < n, 0 < w ≤ 100, u ≠ v) meaning that there is a road between junction u and v and you need w minutes to travel in the road. Your home is in junction 0 and you are initially in your home. And you may safely assume that there can be at most one road between a pair of junctions.

Output

For each case, print the case number and the expected time in minutes. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

3

 

3 2

0 1 3

1 2 3

 

4 6

0 1 75

0 2 86

0 3 4

1 2 1

1 3 53

2 3 10

 

5 5

0 1 10

1 2 20

2 3 30

1 3 20

3 4 10

Case 1: 16

Case 2: 106.8333333333

Case 3: 90

Note

For the 3rd case, initially you are in junction 0, and you can either stay here for 5 minutes, or you can move to 1. The probability of staying in 0 is 0.5 and the probability of going to junction 1 is also 0.5. Now if you are in junction 1, either you can stay here for 5 minutes or you can move to junction 2. From junction 1, you cannot move to junction 3, because if you go to junction 3, you can move to junction 2 or junction 4, but if you go to 2, you cannot visit junction 4 (since police would have occupied junction 3), and if you go to junction 4 from 3, you cannot visit junction 2 for the same reason. So, from 1, junction 2 is the only EJ, but junction 3 is not.

题意:

给你n(n<15)个点,m条无向边及其权值,你是一个刚偷完东西的小偷,身后有警察追你,因此你需要从1号点开始进行逃亡,在每个点你都有≥1个选择:

1、停留在原地5分钟

2、假设你在u号点,与u节点直接相连的点有v1,v2,...,vx号点,如果其中的某些节点到达它后可以通过这个节点遍历所有的点,那么你就可以选择这个点。选择每个合法点的概率是相等的,求你无路可走的时间期望

思路:

1、对于每个点u(编号0~n-1), 先找出所有可选择的点,假设有k个可选点

2、dp[zt][u]表示经过的点的状态为zt,目前在u点时走完所有点的时间期望(v为u的可选点)

dp[zt][u]=\frac{1}{k}*\sum_{v}\left ( dp[zt|(1<<v)] +a[u][v]\right )+\frac{1}{k}*\left ( dp[zt][u]+5 \right )

化简可得:

dp[zt][u]=\frac{\sum_{v}\left ( dp[zt|(1<<v)] +a[u][v]\right )+5}{k-1}

3、至于能否走完所有的点,做一下记忆化搜索就可以了

代码:

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
const int maxn=18;
int n,m,k,mm;
double a[maxn][maxn],dp[(1<<15)+5][18];
bool vis[(1<<15)+5][maxn];
bool ok[(1<<15)+5][maxn];
bool dfs(int zt,int u)
{
    if(zt==mm)
    {
        dp[zt][u]=0;
        return true;
    }
    if(vis[zt][u]) return ok[zt][u];
    vis[zt][u]=true;
    int cnt=0;
    dp[zt][u]=0;
    double &x=dp[zt][u];
    for(int i=0;i<n;i++)
    if(a[u][i]&&(!(zt&(1<<i))))
    {
        if(dfs(zt|(1<<i),i))
        {
            cnt++;
            x+=dp[zt|(1<<i)][i]+a[u][i];
            ok[zt][u]=true;
        }
    }
    if(!ok[zt][u]) return false;
    x=(x+5.0)/cnt;
    return true;
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        mm=(1<<n)-1;
        memset(a,0,sizeof(a));
        memset(vis,0,sizeof(vis));
        memset(ok,0,sizeof(ok));
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            a[x][y]=a[y][x]=z;
        }
        dfs(1,0);
        cout<<"Case "<<cas++<<": ";
        cout<<fixed<<setprecision(10)<<dp[1][0]<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值