Where to Run

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

Hint

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个点(编号0~n-1),m条双向边,且无重边,每条边上都有一个时间的权值.你从点0出发,每次会有若干个选择:

1.停留在当地5分钟;

2.去往别的地点,要求这个地点没有去过,并且从这个地点可以不经过所有之前访问过的点,访问完所有未访问的点.

求出你被逮捕的期望时间.

我们设当前是点i,从这个点出发被逮捕的期望为E[i],有cnt个选择(包括不动),则逆着推得:

E[i]=1/cnt*(sigma(E[j]+w[i][j]))+1/cnt*(E[i]+5),整理得:

E[i]=(sigma(E[j]+w[i][j])+5)/(k-1).

那么,我们可以DFS预处理出每个状态(i,s)中的i是不是那所谓的"junction",如果是的话,选择方案数cnt又是多少.

处理出这个后,我们就可以进行DP/记忆化搜索,如果当前状态(i,s)可行,枚举每个与点i有连边的j,写出转移方程:

E[i][s]+=(E[j][s_new]+w[i][j])/(cnt-1)

最后,E[i][s]+=5/(cnt-1).

最后要求什么?从起点0,状态1的时候的期望,即E[0][1].

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #define sj son[j]
 using namespace std;
 ,maxs=,maxe=;
 int n,m,M,tot,lnk[maxn],nxt[maxe],son[maxe],w[maxe];
 double E[maxn][maxs],cnt[maxn][maxs],ans,inf;
 bool F[maxn][maxs];
 int read(){
     ; char ch=getchar();
     ') ch=getchar();
     +ch-',ch=getchar();
     return x;
 }
 void add(int x,int y,int z){
     nxt[++tot]=lnk[x],son[tot]=y,w[tot]=z,lnk[x]=tot;
 }
 bool Maker(int x,int s){
     ;
     for (int j=lnk[x]; j; j=nxt[j]){
         <<sj))||!Maker(sj,s|(<<sj))) continue;
         F[x][s]=,cnt[x][s]++;
     }
     return F[x][s];
 }
 double DFS(int x,int s){
     ) ;
     ) return E[x][s];
     E[x][s]=;
     for (int j=lnk[x]; j; j=nxt[j]){
         <<sj);
         <<sj))||!F[sj][nows]) continue;
         E[x][s]+=(DFS(sj,nows)+(double)w[j]);
     }
     E[x][s]+=5.0,E[x][s]/=(double)cnt[x][s];
     return E[x][s];
 }
 int main(){
     ; ts<=T; ts++){
         n=read(),m=read(),M=(<<n)-,tot=;
         memset(lnk,,sizeof lnk);
         memset(nxt,,sizeof nxt);
         memset(cnt,,sizeof cnt);
         memset(F,,sizeof F);
         memset(E,,][]-1.0;
         ; i<=m; i++){
             int x=read(),y=read(),z=read();
             add(x,y,z),add(y,x,z);
         }
         Maker(,);
         printf(,));
     }
     ;
 }

[LightOJ 1287] Where to Run的更多相关文章

  1. LightOJ - 1287 Where to Run —— 期望、状压DP

    题目链接:https://vjudge.net/problem/LightOJ-1287 1287 - Where to Run    PDF (English) Statistics Forum T ...

  2. LightOJ 1287 Where to Run(期望)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0.你开始遍历这个图 ...

  3. LightOJ - 1287 Where to Run (期望dp+记忆化)

    题面: Last night you robbed a bank but couldn't escape and when you just got outside today, the police ...

  4. Where to Run LightOJ - 1287(概率dp)

    Where to Run LightOJ - 1287(概率dp) 题面长长的,看了半天也没看懂题意 不清楚的地方,如何判断一个点是否是EJ 按照我的理解 在一个EJ点处,要么原地停留五分钟接着走,要 ...

  5. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  6. kuangbin 带你飞 概率期望

    正推不行就逆推! 经典问题:生日悖论 换成其互斥事件:m个人, 每个人生日都不相同的概率 ≤ 0.5 时最小人数. 这就是邮票收集问题的变形:每个邮票至少出现一次的概率 小于等于 0.5 邮票收集问题 ...

  7. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  8. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  9. LightOJ 1341 唯一分解定理

    Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld &a ...

随机推荐

  1. C#——LINQ语句

    委托: //delegate 返回值 委托名(参数); //委托不能在方法中定义 ////实例化委托,并赋值 //委托名 实例名 = new 委托名(函数名).lambda表达式; //使用委托实例, ...

  2. 解决:springmvc中接收date数据问题

    这里提供三种解决方案. 一.局部转换 :只是对当前Controller类有效 springMVC.xml中添加: <bean class="org.springframework.we ...

  3. JS绘制拓扑图示例 (JTopo)

    目前在做的项目是渔政的监控,需要用到的设备包括雷达,光电,站点信息等,想要更直观的展现设备之间的连接关系和状态信息,这时候需要画一张拓扑图 在做拓扑图之前,首先要学习一下,html里面另一个比较常用的 ...

  4. 因样式冲突引起的div消失问题

    工作需要,搭建一个网站的模型,简单分成三个部分,标题栏,导航栏,主界面,效果如图: 但是点击界面的任意地方,中间的div块消失了,如图所示: 调试,发现在点击界面其他地方的时候display属性有变化 ...

  5. 【二十二】mysqli事务处理与预处理总结

    事务处理 事务基本原理 如果不开启事务,执行一条sql,马上会持久化数据.可见:默认的mysql对sql语句的执行是自动提交的! 如果开启了事务,就是关闭了自动提交的功能,改成了commit执行自动提 ...

  6. python shelve模块

    #coding=utf- import shelve f = shelve.open("shelve_test") f['info'] = "alex" f[, ...

  7. Array、List和ArrayList的区别(推荐: 浅显易懂)

    数组.List和ArrayList的区别(推荐: 浅显易懂)   有些知识点可能平时一直在使用,不过实际开发中我们可能只是知其然不知其所以然,所以经常的总结会对我们的提高和进步有很大的帮助,这里记录自 ...

  8. 学习笔记4-pathon的range()函数和list()函数

    使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 这里记 ...

  9. 在cmd中登录MySQL数据库

    mysql -uroot -p 输入密码,即可

  10. centos如何查看linux内核,版本号

    [root@localhost ~]# uname -a Linux localhost.localdomain -.el7.x86_64 # SMP Thu Nov :: UTC x86_64 x8 ...