LightOJ - 1321 Sending Packets —— 機率指望

題目連接:https://vjudge.net/problem/LightOJ-1321php

 

1321 - Sending Packets
Time Limit: 2 second(s) Memory Limit: 32 MB

Alice and Bob are trying to communicate through the internet. Just assume that there are N routers in the internet and they are numbered from 0 to N-1. Alice is directly connected to router 0 and Bob is directly connected to router N-1. Alice initiates the connection and she wants to send S KB of data to Bob. Data can go to the (N-1)th router from the 0th router either directly or via some intermediate routers. There are some bidirectional links between some routers.ios

The links between the routers are not necessarily 100% perfect. So, for each link, a probability pi is given. That means if u and v are two routers and if their underlying link has probability pi, it means that if data is sent from u to v, the probability of successfully getting the data in v is pi and vice versa. If multiple links are used the probability of getting the data in destination is the multiplication of the probabilities of the links that have been used.算法

Assume that it takes exactly K seconds for a packet to reach Bob's router from Alice's router (independent on the number of links) if it's successful. And when the data is successfully received in Bob's router, it immediately sends an acknowledgement to Alice's router and the acknowledgement always reaches her router exactly in K seconds (it never disappears).app

Alice's router used the following algorithm for the data communication.less

1)      At time 0, the first KB of data is chosen to be sent.ide

2)      It establishes a path (it takes no time) to the destination router and sends the data in this route.this

3)      It waits for exactly 2K seconds.spa

  1. If it gets the acknowledgement of the current data in this interval
    1.                                                               i.      If S KB of data are sent, then step 4 is followed.
    2.                                                             ii.      Otherwise, it takes 1 KB of the next data, and then step 2 is followed.
  2. Otherwise it resends the current 1 KB of data and then step 2 is followed.

4)      All the data are sent, so it reports Alice..net

Assume that the probabilities of the links are static and independent. That means it doesn't depend on the result of the previously sent data. Now your task is to choose some routes through the routers such that data can be sent in these routes and the expected time to send all the data to the destination routes is minimized. You only have to report the minimum expected time.3d

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing four integers N (2 ≤ N ≤ 100)M (1 ≤ M)S (1 ≤ S ≤ 109) and K (1 ≤ K ≤ 20), where M denotes the number of bidirectional links. Each of the next M lines contains three integers ui vi pi, meaning that there is a link between router ui and vi the probability for a successful message transfer in this link is pi% (0 ≤ ui, vi < N, ui ≠ vi, 0 < pi ≤ 100). There will be at most one link between two routers.

Output

For each case, print the case number and the minimum possible expected time to send all the data. Errors less than 10-3 will be ignored. You can assume that at least one valid route between them always exists. And the result will be less than 1013.

Sample Input

Output for Sample Input

2

5 5 1 10

0 1 70

0 2 40

2 3 100

1 3 50

4 3 80

2 1 30 2

0 1 80

Case 1: 62.5000000000

Case 2: 150

Note

For sample 1, we get the following picture. We send the data through 0 - 2 - 3 - 4.

 

 

題意:

給出一張圖,從0到n-1傳輸s個包,傳輸的時候每條邊正常運做的機率爲pi,每次傳輸的時間爲2K。若是可以運到終點,則還需從終點回到起始點;若是不能運到終點,則要從當前點返回到起始點(走過的路確保暢通),而後繼續運送。每次往返的固定時間爲2K,求最小的傳送時間。

 

題解:

1. 用最短路算法求出從起點到終點的最大機率p。

2 先求出運輸單個包所用的平均時間:EX = p*2K + (1-p)*(2K+EX),移項得:EX = 2K/p。再乘上s個,則答案爲:2K*s/p 。

 

 

代碼以下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const double EPS = 1e-6;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e5;
18 const int MAXN = 1e2+10;
19 
20 double g[MAXN][MAXN];
21 
22 queue<int>Q;
23 double dis[MAXN], in[MAXN];
24 double spfa(int n)
25 {
26     memset(dis, 0, sizeof(dis));
27     memset(in, 0, sizeof(in));
28     while(!Q.empty()) Q.pop();
29 
30     dis[0] = 1.0;
31     Q.push(0);
32     while(!Q.empty())
33     {
34         int u = Q.front();
35         Q.pop();
36         in[u] = false;
37         for(int v = 0; v<n; v++)
38         {
39             if(dis[v]<dis[u]*g[u][v])
40             {
41                 dis[v] = dis[u]*g[u][v];
42                 if(!in[v])
43                 {
44                     in[v] = true;
45                     Q.push(v);
46                 }
47             }
48         }
49     }
50     return dis[n-1];
51 }
52 
53 int main()
54 {
55     int T, kase = 0;
56     int n, m, k, s;
57     scanf("%d", &T);
58     while(T--)
59     {
60         scanf("%d%d%d%d", &n,&m,&k,&s);
61         memset(g, 0, sizeof(g));
62         while(m--)
63         {
64             int u, v, w;
65             scanf("%d%d%d", &u,&v,&w);
66             g[u][v] = g[v][u] = 0.01*w;
67         }
68 
69         double p = spfa(n);
70         double ans = 2.0*k/p*s;
71         printf("Case %d: %.8lf\n", ++kase, ans);
72     }
73 }
View Code
相關文章
相關標籤/搜索