题解:P4745 [CERC2017] Gambling Guide
记
考虑转移式:
记
上式可以整理为:
然后,考虑一个类 Dij 算法:
-
初始
f_n=0 ,f_i=\infty 。 -
每次直接把当前的
f_u 最小值确定下来,然后此时没有确定的f_x 以后也会>f_u ,所以我们把u 贡献到那些x 的k_x,sum_x 中。 -
重复下去,直到
f_1 算出。
考虑证明正确性。
首先,
然后考察
为我们已知的是
所以,我们用
也就是说当前最小值就是一个已经确定最小的状态,其它值能更新它,只会也不会更新得比它小。
复杂度
:::info[Code]
#include <bits/stdc++.h>
using namespace std;
#define re register
namespace Fastio{
#define SIZIO (1<<22)
#define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,SIZIO,stdin))==rp1?EOF:*rp1++)
char buf[SIZIO+1],*rp1,*rp2;
inline int read(){
int d=0,f=0;char ch=gc();
while (!isdigit(ch)) f|=(ch=='-'),ch=gc();
while (isdigit(ch)) d=(d<<1)+(d<<3)+ch-'0',ch=gc();
return f?-d:d;
}
};using namespace Fastio;
typedef pair<double,int> PDI;
const int N=300005;
struct Edge{int t,ne;}e[N<<1];
int n,m,h[N],idx=1,k[N],cnt[N];
double sum[N],f[N];
bool vis[N];
inline void add(int a,int b) {e[idx]={b,h[a]};h[a]=idx++;}
priority_queue <PDI,vector<PDI>,greater<PDI> > q;
inline void dijkstra(){
q.push({0,n});
while (!q.empty()){
int u=q.top().second;q.pop();
if (vis[u]) continue;
if (u==1) break;
vis[u]=1;
for (re int i=h[u];i;i=e[i].ne){
int v=e[i].t;
if (vis[v]) continue;
cnt[v]++,sum[v]+=f[u];
f[v]=(k[v]+sum[v])/cnt[v];
q.push({f[v],v});
}
}
}
int main(){
n=read(),m=read();
for (re int i=1;i<=m;i++){
int a=read(),b=read();
add(a,b),add(b,a),k[a]++,k[b]++;
}
dijkstra();
printf("%.7lf",f[1]);
return 0;
}
:::