Sending Packets LightOJ

来源:互联网 发布:动态恐怖漫画软件 编辑:程序博客网 时间:2024/06/07 14:24

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.

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

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-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
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
Hint
For sample 1, we get the following picture. We send the data through 0 - 2 - 3 - 4.

大致题意:一个大小为s的数据包需要在一张图上从0点传送到n-1点去,每条边有个传输成功的概率,开始是先传送1的数据,如果成功到达n-1点的话,那么这条路就通了,剩下的数据就可以一直用这条路径来传递而且成功率为1.0。如果没能成功到达,就继续传送1的数据,直到成功为止。如果无法通过,那么就要回到起点重新出发
从起点到终点的时间固定为K,如果成功到达,又需要额外花费K的时间,
求最小传输完的时间期望。

思路:先跑一遍Floyd,求出一次通过的最大概率p,假设跑一次通过的最小时间期望为E,那么有
E=p*2*k+(1-p)*(E+2*k),解得E=2*k/p,最后答案再乘上s

代码如下

#include<iostream>#include<cstdio>#include<queue>#include<cmath>#include<cstring>using namespace std;#define LL long long #define ULL unsigned long long const double eps=1e-7;double dis[105][105];int main() {    int T;    int n,m,s,k;    scanf("%d",&T);    for(int cas=1;cas<=T;cas++)    {        memset(dis,0,sizeof(dis));        scanf("%d%d%d%d",&n,&m,&s,&k);        while(m--)        {            int x,y,c;            scanf("%d%d%d",&x,&y,&c);            dis[x][y]=dis[y][x]=c*1.0/100;        }        for(int i=0;i<n;i++)        dis[i][i]=1.0;        for(int k=0;k<n;k++)        for(int i=0;i<n;i++)        for(int j=0;j<n;j++)        dis[i][j]=max(dis[i][j],dis[i][k]*dis[k][j]);        double ans=dis[0][n-1];        ans=1.0/ans*2*k*s;        printf("Case %d: %.4lf\n",cas,ans);    }    return 0;}
原创粉丝点击