# [省选练习]Path：期望DP+Dijkstra

## 题意

$n,m \leq 10^5$

## 分析

$dis[x]$为从$x$$n$的期望时间。

$dis[x]=\frac{m}{cnt[x]}+\frac{sum[x]}{cnt[x]}$

## 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <vector>
#include <queue>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl;

int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}

const int MAXN=100005;
double dis[MAXN],sum[MAXN];
bool vis[MAXN];
struct Edge{
int to,nxt;
}e[MAXN<<1];
struct Pair{
int pos;
double dis;
inline friend bool operator > (Pair x,Pair y){
return x.dis>y.dis;
}
};
std::priority_queue<Pair,std::vector<Pair>,std::greater<Pair> > q;

ecnt++;
e[ecnt].to=ed;
}

inline void dijkstra(){
rin(i,1,n) dis[i]=1e12;
while(!q.empty()) q.pop();
dis[n]=0;
q.push((Pair){n,0});
while(!q.empty()){
int x=q.top().pos;q.pop();
if(vis[x]) continue;
vis[x]=1;
trav(i,x){
int ver=e[i].to;
if(!vis[ver]&&dis[ver]>dis[x]){
sum[ver]+=dis[x];
cnt[ver]++;
dis[ver]=m*1.0/cnt[ver]+sum[ver]/cnt[ver];
q.push((Pair){ver,dis[ver]});
}
}
}
}

int main(){
freopen("path.in","r",stdin);
freopen("path.out","w",stdout);
rin(i,1,m){
}
dijkstra();
printf("%.10lf\n",dis[1]);
return 0;
}

/*
4 4
1 2
2 4
1 3
3 4

6.0000000000
*/


posted on 2018-12-25 21:43  ErkkiErkko  阅读(14)  评论(0编辑  收藏

• 随笔 - 91
• 文章 - 0
• 评论 - 20