Sending Packets LightOJ - 1321 (期望计算)

题面:
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.
题意:
Alice要通过一个路由器网络发送\(s\)个报文,发送一个报文的时间是\(2*k\),发送一个报文后,收到确定报文后才会发送下一个。发送时的成功几率并非100%,求最后发送完所有报文的期望。
思路:
对于一个路径来说,成功的几率是固定的。
所以将几率用最短路求出即可。
发送第\(x\)个报文成功的期望为:

\[E_x = p*E_{x-1}+(1-p)*E_x+2*k \]

移项可得:

\[E_x =\frac{2*s*k}{p} \]

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>

#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 100086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

int Head[maxm],cnt;
struct edge{
    int Next,v;
    double w;
}e[maxm];
struct node{
    int u;
    double dis;
    bool operator<(const node &a)const{
        return a.dis>dis;
    }
};
double dis[maxn];
bool vis[maxn];
void init(){
    memset(Head,-1,sizeof(Head));
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    cnt=0;
}
void add_edge(int u,int v,double w){
    e[cnt].Next=Head[u];
    e[cnt].v=v;
    e[cnt].w=w;
    Head[u]=cnt++;
}
void Dijkstra(int s){
    priority_queue<node>q;
    q.push(node{s,0});
    dis[s]=1;
    while(!q.empty()){
        node exa=q.top();q.pop();
        int u=exa.u;
        if(vis[u]){continue;}
        vis[u]=true;
        for(int k=Head[u];k!=-1;k=e[k].Next){
            if(!vis[e[k].v]&&dis[e[k].v]<dis[u]*e[k].w){
                dis[e[k].v]=dis[u]*e[k].w;
                q.push(node{e[k].v,dis[e[k].v]});
            }
        }
    }
}


int main() {
    ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int T;
    scanf("%d",&T);
    int cas = 0;
    while(T--){
        init();
        ll n,m,s,k;
        scanf("%lld%lld%lld%lld",&n,&m,&s,&k);

        for(int i=1;i<=m;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add_edge(x,y,1.0*z/100);
            add_edge(y,x,1.0*z/100);
        }
        Dijkstra(0);
        double p = dis[n-1];
        double ans = 2.0*s*k/p;
        printf("Case %d: %f\n",++cas,ans);
    }

    return 0;
}

posted @ 2019-08-23 09:41  断腿三郎  阅读(228)  评论(0编辑  收藏  举报