[LightOJ 1287] Where to Run

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].

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define sj son[j]
 5 using namespace std;
 6 const int maxn=16,maxs=36005,maxe=1005;
 7 int n,m,M,tot,lnk[maxn],nxt[maxe],son[maxe],w[maxe];
 8 double E[maxn][maxs],cnt[maxn][maxs],ans,inf;
 9 bool F[maxn][maxs];
10 int read(){
11     int x=0; char ch=getchar();
12     while (ch<'0'||ch>'9') ch=getchar();
13     while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
14     return x;
15 }
16 void add(int x,int y,int z){
17     nxt[++tot]=lnk[x],son[tot]=y,w[tot]=z,lnk[x]=tot;
18 }
19 bool Maker(int x,int s){
20     if (s==M||F[x][s]) return F[x][s]=1;
21     for (int j=lnk[x]; j; j=nxt[j]){
22         if ((s&(1<<sj))||!Maker(sj,s|(1<<sj))) continue;
23         F[x][s]=1,cnt[x][s]++;
24     }
25     return F[x][s];
26 }
27 double DFS(int x,int s){
28     if (s==M||cnt[x][s]==0) return 0;
29     if (E[x][s]<inf&&E[x][s]>0) return E[x][s];
30     E[x][s]=0;
31     for (int j=lnk[x]; j; j=nxt[j]){
32         int nows=s|(1<<sj);
33         if ((s&(1<<sj))||!F[sj][nows]) continue;
34         E[x][s]+=(DFS(sj,nows)+(double)w[j]);
35     }
36     E[x][s]+=5.0,E[x][s]/=(double)cnt[x][s];
37     return E[x][s];
38 }
39 int main(){
40     for (int T=read(),ts=1; ts<=T; ts++){
41         n=read(),m=read(),M=(1<<n)-1,tot=0;
42         memset(lnk,0,sizeof lnk);
43         memset(nxt,0,sizeof nxt);
44         memset(cnt,0,sizeof cnt);
45         memset(F,0,sizeof F);
46         memset(E,127,sizeof E),inf=E[0][0]-1.0;
47         for (int i=1; i<=m; i++){
48             int x=read(),y=read(),z=read();
49             add(x,y,z),add(y,x,z);
50         }
51         Maker(0,1);
52         printf("Case %d: %.8lf\n",ts,DFS(0,1));
53     }
54     return 0;
55 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7213944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值