LightOJ - 1321 - Sending Packets (spfa+期望dp)

1321 - Sending Packets

   PDF (English)StatisticsForum
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.

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

题意:

给你n(n<=100)个点(0~n-1),m(>=1)条边,以及每条边通过的概率百分比,起点为0号点,终点为n-1号点,如果在到达终点的途中某条边无法通过,那么就要回到起点重新出发。
从起点到终点的时间固定为k,如果成功到达,又需要额外花费k的时间,问走s次的最小期望时间。

思路:

首先可以直接用spfa求出起点到终点的最大成功概率(即找一条到达终点最大概率的路径)

设一次通过的期望时间为E,最大成功概率为p,则

E=p*2*k+(1-p)*(E+2*k)

化简得E=2k/p,最后再乘s即可。

代码:

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
const int maxn=210;
int n,m;
double k,s;
int he[maxn],cnt;
double d[maxn];
bool vis[maxn];
struct node
{
    int v,nxt;
    double w;
}e[maxn*maxn];
void add(int u,int v,double w)
{
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].nxt=he[u];
    he[u]=cnt++;
}
void init()
{
    cnt=0;
    memset(he,-1,sizeof(he));
}
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=he[u];i!=-1;i=e[i].nxt)
        {
            int v=e[i].v;
            if(d[u]*e[i].w>d[v])
            {
                d[v]=d[u]*e[i].w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d%lf%lf",&n,&m,&s,&k);
        for(int i=0;i<m;i++)
        {
            int x,y;
            double z;
            scanf("%d%d%lf",&x,&y,&z);
            z/=100.0;
            add(x,y,z);
            add(y,x,z);
        }
        spfa();
        double ans=2.0*k*s/d[n-1];
        cout<<"Case "<<cas++<<": ";
        cout<<fixed<<setprecision(10)<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值