LightOJ1287 --- Where to Run (probability dp)

Source: Internet
Author: User
Tags bitset cmath

LightOJ1287 --- Where to Run (probability 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 wocould 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 J. 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 shoshould 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 wowould 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 wowould 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.

An undirected graph starts from 0 and observes each time. The graph goes from the current vertex to the next vertex. If you can traverse all vertices from that vertex, the vertex becomes
If the number of EPCs is 0, no action is taken.
Otherwise, you can choose to go to any EJ, or stay in the same place for 5 minutes.
The probability is the same, and the expected time for traversing the graph is obtained.

Obvious probability dp
Dp [u] [sta] Indicates the desired time for traversing the graph when the current vertex u is passed and the vertices are in the sta state.
To determine the number of the EJ points, you can store the dfs in advance and remember to perform the Memory search.

/*************************************** * *********************************> File Name: k. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: monday ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; vector
                
                  Roads [20]; int n; double dp [16] [(1 <15) + 10]; int OK [16] [(1 <15) + 10]; double DP (int u, int sta) {double & res = dp [u] [sta]; if (res! =-1.0) {return res;} int cnt = 0; int size = roads [u]. size (); for (int I = 0; I <size; ++ I) {int v = roads [u] [I]. first; if (sta & (1 <v) {continue;} if (OK [v] [sta | (1 <u)] = 1) {++ cnt ;}} if (! Cnt) {return res = 0;} res = 0; double p = 1.0/(1 + cnt); for (int I = 0; I <size; ++ I) {int v = roads [u] [I]. first; if (sta & (1 <v) {continue;} if (! OK [v] [sta | (1 <u)]) {continue;} int w = roads [u] [I]. second; res + = p * w + p * DP (v, sta | (1 <u);} res + = 5.0 * p; res/= (1-p); return res;} void dfs (int u, int sta) {if (sta | (1 <u )) = (1 <n)-1) {OK [u] [sta] = 1; dp [u] [sta] = 0; return ;} OK [u] [sta] = 0; int size = roads [u]. size (); for (int I = 0; I <size; ++ I) {int v = roads [u] [I]. first; if (! (Sta & (1 <v) {if (OK [v] [sta | (1 <u)] =-1) {dfs (v, sta | (1 <u ));} OK [u] [sta] = (OK [u] [sta] | OK [v] [sta | (1 <u)]) ;}} int main () {int t, icase = 1; scanf ("% d", & t); while (t --) {int m; scanf ("% d", & n, & m); for (int I = 0; I <n; ++ I) {roads [I]. clear () ;}int u, v, w; for (int I = 0; I <m; ++ I) {scanf ("% d ", & u, & v, & w); roads [u]. push_back (make_pair (v, w); roads [v]. push_back (make_pair (u, w) ;}for (int I = 0; I <(1 <n); ++ I) {for (int j = 0; j <n; ++ j) {dp [j] [I] =-1.0; OK [j] [I] =-1 ;}} dfs (0, 0 ); printf ("Case % d: %. 12f \ n ", icase ++, DP (0, 0);} return 0 ;}
                
               
              
             
            
          
         
        
       
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.