LightOJ 1321 Sending Packets【期望+SPFA】

本文讨论了Alice和Bob通过互联网进行数据通信的问题,其中数据包在路由器之间传输的成功率由特定的概率pi给出。目标是找到一条路径,使得所有数据能够以最小的预期时间到达目的地。给出了输入输出格式以及样例输入输出,并提供了问题的解析和解决方案,即求解最大一次性传输成功率对应的最小时间期望。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参看资料:

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值