LightOJ 1321 Sending Packets【期望+SPFA】

參看資料:

https://blog.csdn.net/qq_31759205/article/details/54716066


題目:

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 0throuter either directly or via some intermediate routers. There are some bidirectional links between some routers.

The links between the routers are not necessarily 100% perfect. So, for each link, a probability pi is given. That means if uand 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).

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

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

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

3) It waits for exactly 2K seconds.

a. If it gets the acknowledgement of the current data in this interval

 i. If S KB of data are sent, then step 4 is followed.

 ii.Otherwise, it takes 1 KB of the next data, and then step 2 is followed.

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

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.

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-3will 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

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

Sample Output

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,如果在傳輸時候沒有安全到達,則要重新傳送,求最小的傳送時間;

解題思路:

       只要成功傳輸一次,剩下的就能快速傳輸,不計時間。

      那麼求最小時間期望就 等同於 求最大的一次性傳輸成功的概率p對應的時間期望,傳輸過程符合幾何分佈,那麼傳輸成功的最小次數的期望就是1/p,那麼最小的時間期望就是   1/p * 2 * k * s。

實現代碼:

#include<bits/stdc++.h>
using namespace std;
const int MX=105;
struct Edge{
    int v,nxt;
    double c;
}edge[MX*MX];

int head[MX],tot;
void add(int u,int v,double c){
    edge[tot].v=v;
    edge[tot].c=c;
    edge[tot].nxt=head[u];
    head[u]=tot++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
double d[MX];
bool vis[MX];

void spfa(){
    memset(vis,0,sizeof(vis));
    memset(d,0,sizeof(d));
    queue<int>q;
    q.push(0);
    vis[0]=1;
    d[0]=1;
    while(!q.empty()){
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];~i;i=edge[i].nxt){
            int v=edge[i].v;
            double c=edge[i].c;
            if(d[v]>=d[u]*c) continue;
            d[v]=d[u]*c;
            if(vis[v]) continue;
            vis[v]=1;
            q.push(v);
        }
    }
}
int main(){
    int T,n,m,k,s;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        init();
        scanf("%d%d%d%d",&n,&m,&s,&k);
        for(int i=0;i<m;i++){
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            add(u,v,c/100.0);
            add(v,u,c/100.0);
        }
        spfa();
        double ans=2.0*s*k/d[n-1];
        printf("Case %d: %.7f\n",cas,ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章