图上期望——Where to Run LightOJ - 1287 期望+记忆化dfs+状态压缩

博客目录

返回二级目录——kaugnbin概率dp习题集

原题

传送门

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/4 or 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

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

Sample Output

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+1个节点标号为:0,1,2,...n,有m个边,每个边都有自己的权值。

你初始在0点。不能重复走点,接下来选择的路必须保证可以遍历整个图而不重复,否则就不能走。每个可以走的路和在当前点呆5分钟都是等可能的,经过一条路花费的时间等于边权。问遍历整个图所需时间的期望。

思路

经过简单的公式推导(不会的先看看我博客其他题,很基础的推导),每个点对期望的贡献为每条可走的路之后的期望贡献+5再整个除以可走的路条数。记得状态压缩和记忆化搜索。

AC代码

#include<bits/stdc++.h>
#include<cstring>
using namespace std;
typedef long double lb;
int rd[17][17];
vector<int>mp[17];
int n,m;
lb mem[17][1025*64];
bool c[17][1025*64];
int b[17];
int end;
lb dfs(int s,int rt){
    if(c[rt][s]){
        return mem[rt][s];
    }
    if(s==end){
        c[rt][s]=1;
        return mem[rt][s]=5;
    }
    int sz=mp[rt].size();
    int cnt=0;
    lb ret=5,t;
    for(int i=0;i<sz;i++){
        int cu=mp[rt][i];
        if(b[cu]&s){
            continue;
        }
        t=dfs(s|b[cu],cu);
        if(t){
            ret+=t+rd[rt][cu];
            cnt++;
        }
    }
    c[rt][s]=1;
    if(rt==0 && cnt==0){
        return 5; //0这个点特判,小心外面减成-5 
    }
    if(cnt){
        return mem[rt][s]=ret/cnt;
    }
    else return mem[rt][s]=0;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("r.txt","r",stdin);
    #endif
    int T;
    b[0]=1;
    for(int i=1;i<=16;i++)
        b[i]=b[i-1]<<1;
    int x,y,w,cs=1;
    cin>>T;
    while(T--){
        scanf("%d%d",&n,&m);
        end=0;
        for(int i=0;i<n;i++){
            end|=b[i];
        }
        for(int i=0;i<n;i++){
            mp[i].clear();
        }
        memset(rd,0,sizeof(rd));
        memset(c,0,sizeof(c));
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&x,&y,&w);
            rd[x][y]=rd[y][x]=w;
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        printf("Case %d: %.8Lf\n",cs++,dfs(1,0)-5);
    } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值