Where to Run LightOJ

来源:互联网 发布:互联网mysql开发规范 编辑:程序博客网 时间:2024/06/10 14:21

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,有m条有权无向边,你从0号点出发, 每个城市只能走一遍,每次你有两种选择,1.在当前城市停留5分钟,2.假设有cnt个城市,满足当前城市相邻,且能走完剩下的所有城市,然后你等概率的选择其中一个城市前往。停留在当前城市和前往其他某个城市的概率均为1/(cnt+1).问你遍历完所有城市所需的期望时间。

思路:注意到n<=15,所以我们用状压来写,假设dp[u][state]表示到达点u状态为state时遍历完城市所需期望时间,cnt为此时满足条件的点,dp[u][state]=(5+dp[u][state])/(1+cnt) + (dis[u][i]+dp[i][state|(1<< i)])/(1+cnt) (i为满足条件的其他城市的编号)

代码如下

#include<bits/stdc++.h>using namespace std;#define LL long long const int N=15;double dp[N][1<<N];//dp[u][state] 表示在点u,state的情况下完成的期望时间 int vis[N][1<<N];int state;struct Edge{    int v,cost;    int next;}edge[N*N];int tol;int head[N];int n;void addedge(int u,int v,int w){    edge[tol].v=v;    edge[tol].cost=w;    edge[tol].next=head[u];    head[u]=tol++;}int dfs(int u,int st)//到u点,状态为st时{    if(st==(1<<n)-1)//如果此时已经将所有点走过一遍    {        dp[u][st]=0;//到达u点,在该状态下的期望时间为0        return 1;//到达u点,在该状态下能将所有点走过    }    if(vis[u][st])     {        if(dp[u][st]>0) return 1;        else return 0;    }    vis[u][st]=1;    int cnt=0;//记录所能到达且满足条件的点的个数    dp[u][st]=5.0;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int to=edge[i].v;        if(!(st&(1<<to))&&dfs(to,st|(1<<to)))        {            cnt++;                 dp[u][st]+=edge[i].cost+dp[to][st|(1<<to)];          }    }    if(!cnt)    {        dp[u][st]=0;        return 0;    }    dp[u][st]/=cnt;    return 1;}void init(){    tol=0;    memset(head,-1,sizeof(head));    memset(dp,0,sizeof(dp));    memset(vis,0,sizeof(vis));}int main(){    int T;    scanf("%d",&T);    int m;    for(int cas=1;cas<=T;cas++)    {        init();        scanf("%d%d",&n,&m);        int a,b,c;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&a,&b,&c);                          addedge(b,a,c);            addedge(a,b,c);        }        dfs(0,1);        printf("Case %d: %.7lf\n",cas,dp[0][1]);    }    return 0;}