题解 UVA12230 过河 Crossing Rivers
题目大意
从家((A))到公司((B))的直线路段上有 (n) 条河,每条河有三个信息:和 (A) 的距离 (p)、宽度 (L),以及船在这条河上的速度 (v)。船在两岸往返。 在陆地上的速度为 (1),求出从家到公司的期望时间。
题目思路
对于每条河,有最好和最坏两种情况:
-
最好:到达这条河时刚好坐上船,(t= frac{L}{v})。
-
最坏:到达这条河时船刚刚走,(t=3 imes frac{L}{v})。
仔细思考可以知道,船的发布是均匀的,可以利用期望的线性性质得到每条河的平均过河时间 (t=2 imes frac{L}{v})。
期望的线性性质:
(E(X+Y)=E(X)+E(Y))
(E(aX)=aE(X))
当 (X) 和 (Y) 相互独立时,(E(XY)=E(X) imes E(Y))
设初始答案 (ans) 为全走陆地的时间 (D),
则每次输入时去河的长度 (L),加上渡河期望时间 (2 imesfrac{L}{v}) 即可。
(Code)
#include<bits/stdc++.h>
using namespace std;
int n,D;
int p,L,v;
int cnt=0;
double ans;
int main()
{
while(scanf("%d%d",&n,&D))
{
if(n==0&&D==0) break;
ans=D;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&p,&L,&v);
ans-=L;
ans+=(double)2*L/v;
}
printf("Case %d: %.3lf
",++cnt,ans);
}
return 0;
}