Description
Input
Output
Sample Input
3 2 3 3
2 1 2
1 2 1
0.8 0.2 0.5
1 2 5
1 3 3
2 3 1
Sample Output
2.80
The Solution
比较简单的一道期望题。。。不过noip考期望还真是坑爹啊。。不是说好不考期望吗QAQ
很显然我们可以考虑dp
令
f[i][j][0/1]
表示当前在第i个时间段,已经申请了j次,这个时间段是否申请的最小期望
我们发现两堂课之间的期望是独立的,只和两端的状态有关,根据期望的线性我们可以直接把它加起来。
然后就可以dp了,随便推推转移式即可。
至于最短路,考试的时候我打得是spfa 智商下线。。
后来发现直接弗洛伊德就好了。。。
CODE
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define N 2005
using namespace std;
typedef double db;
const db INF = 2147483647.00;
int n,m,v,e,tot = 0;
int c[N],d[N];
db dis[N][N],f[N][N][2],a[N],ans = INF;
void Freud()
{
fo(k,1,v)
fo(i,1,v)
fo(j,1,v)
if (dis[i][j] > dis[i][k] + dis[k][j]) dis[i][j] = dis[i][k] + dis[k][j];
}
int main()
{
//freopen("classroom.in","r",stdin);
// freopen("classroom.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&v,&e);
fo(i,1,n) scanf("%d",&c[i]);
fo(i,1,n) scanf("%d",&d[i]);
fo(i,1,n) scanf("%lf",&a[i]);
fo(i,1,2000) fo(j,0,2000) if (i != j) dis[i][j] =INF , dis[i][i] = 0;
fo(i,1,e)
{
int x,y;
db z;
scanf("%d%d%lf",&x,&y,&z);
dis[x][y] = dis[y][x] = min(dis[x][y],z);
}
Freud();
fo(i,0,2000) fo(j,0,2000) f[i][j][0] = f[i][j][1] = INF;
a[0] = 1;
f[0][0][0] = 0;
fo(i,1,n)
fo(j,0,min(i,m))
{
f[i][j][0] = min(f[i][j][0],f[i-1][j][0] + dis[c[i-1]][c[i]]);
if(j >= 1)
{
db x = (f[i-1][j][1] + dis[d[i-1]][c[i]]) * a[i-1] + (f[i-1][j][1] + dis[c[i-1]][c[i]]) * (1 - a[i-1]);
f[i][j][0] = min(f[i][j][0],x);
x = (f[i-1][j-1][0] + dis[c[i-1]][d[i]]) * a[i] + (f[i-1][j-1][0] + dis[c[i-1]][c[i]]) * (1 - a[i]);
f[i][j][1] = min(f[i][j][1],x);
if(j >= 2)
{
x = (f[i-1][j-1][1] + dis[d[i-1]][d[i]]) * a[i] * a[i-1] +
(f[i-1][j-1][1] + dis[d[i-1]][c[i]]) * (1 - a[i]) * a[i-1] +
(f[i-1][j-1][1] + dis[c[i-1]][d[i]]) * a[i] * (1 - a[i-1]) +
(f[i-1][j-1][1] + dis[c[i-1]][c[i]]) * (1 - a[i]) * (1 - a[i-1]);
f[i][j][1] = min(f[i][j][1],x);
}
}
if (i == n) ans = min(ans,min(f[i][j][1],f[i][j][0]));
}
printf("%.2lf\n",ans);
return 0;
}