[gym101620G] Gambling Guide

Time limit: 3 s
Memory limit: 512 MiB

A railroad network in a nearby country consists of n n n cities numbered 1 1 1 through n n n , and m m m two-way
railroad tracks each connecting two different cities. Tickets can only be purchased at automated
machines installed at every city. Unfortunately, hackers have tampered with the ticket machines and
now they all work as follows: when a single coin is inserted in the machine installed at city a a a , the
machine dispenses a single one-way ticket from a a a to a random neighboring city. More precisely, the
destination city is chosen uniformly at random among all cities directly connected to a with a railroad track. Destinations on different tickets originating in the same city are independent.A computer science student needs to travel from city 1 1 1 (where she lives) to city n n n (where a regional
programming contest has already started). She knows how the machines work (but of course cannot predict the random choices) and has a map of the railway network. In each city, when she purchases a ticket, she can either immediately use it and travel to the destination city on the ticket, or discard the ticket and purchase a new one. She can keep purchasing tickets indefinitely. The trip is finished as soon as she reaches city n n n.
After doing some calculations, she has devised a traveling strategy with the following properties:
• The probability that the trip will eventually finish is 1.
• The expected number of coins spent on the trip is the smallest possible.
Find the expected number of coins she will spend on the trip.

Input
The first line contains two integers n n n and m ( 1 ≤ n , m ≤ 300000 ) m (1 ≤ n , m ≤ 300000) m(1n,m300000) — the number of cities and the number of railroad tracks. Each of the following m lines contains two different integers a and b
(1 ≤ a , b ≤ n ) which describe a railroad track connecting cities a and b . There will be at most one
railroad track between each pair of cities. It will be possible to reach city n starting from city 1.
Output
Output a single number — the expected number of coins spent. The solution will be accepted if the
absolute or the relative difference from the judges solution is less than 1 0 − 6 10^{−6} 106 .

Example
input

4 4
1 2
1 3
2 4
3 4

output

3.0000000000

input

5 8
1 2
1 3
1 4
2 3
2 4
3 5
5 4
2 5

output

4.1111111111

题意:
给定一个图,你刚开始在1点。设你当前在a点,你每次可以随机买一张到与a点相邻的点的票。你可以选择继续买,或者使用这个票去往下一个点。问从1点到n点的期望买票次数最小是多少。

题解:
f [ x ] f[x] f[x]为从 x x x n n n的期望步数

f [ x ] = ∑ ( x , y ) ∈ e min ⁡ ( f [ x ] , f [ y ] ) d u [ x ] + 1 f[x] = \frac { \sum \limits_{(x,y) \in e} \min(f[x] , f[y])}{du[x]} + 1 f[x]=du[x](x,y)emin(f[x],f[y])+1

d u [ x ] f [ x ] = d [ x ] + ∑ ( x , y ) ∈ e [ f [ y ] < f [ x ] ] f [ y ] + ( d [ x ] − ∑ ( x , y ∈ e ) [ f [ y ] < f [ x ] ] ) f [ x ] du[x]f[x] = d[x]+\sum \limits_{(x,y) \in e} [f[y]<f[x]]f[y] + (d[x]-\sum \limits_{(x,y \in e)} {[f[y]<f[x]]}) f[x] du[x]f[x]=d[x]+(x,y)e[f[y]<f[x]]f[y]+(d[x](x,ye)[f[y]<f[x]])f[x]

f [ x ] = d u [ x ] + ∑ ( x , y ) ∈ e [ f [ y ] < f [ x ] ] f [ y ] ∑ ( x , y ) ∈ e [ f [ y ] < f [ x ] ] f[x] = \frac { du[x] + \sum \limits_{(x,y) \in e} [f[y] < f[x]]f[y]}{\sum\limits_{(x,y) \in e} [f[y] < f[x]]} f[x]=(x,y)e[f[y]<f[x]]du[x]+(x,y)e[f[y]<f[x]]f[y]

然后所有 f [ y ] < f [ x ] f[y]<f[x] f[y]<f[x]都可以更新 f [ x ] f[x] f[x]
接着就是直接最短路型dp即可。
记住要倒着dp,否则期望会有后效性。
l c [ x ] lc[x] lc[x]表示在这个状态下达到 f [ x ] f[x] f[x]最小的 f [ y ] < f [ x ] f[y]<f[x] f[y]<f[x] y y y的数目

#include<bits/stdc++.h>
#define ll long long
#define pa pair<double,int>
using namespace std;
int n,m;
double f[300004],lc[300004];
bool vis[300004];
vector<int>G[300004];
priority_queue<pa,vector<pa>,greater<pa> >q;
void dijkstra(int st){
    q.push({0,st});
    while(!q.empty()){
        int x=q.top().second;
        double prop=q.top().first;
        q.pop();
        if(vis[x])continue;
        vis[x]=1;
        f[x]=prop;
        for(int i=0;i<G[x].size();i++){
            int to=G[x][i];
            if(vis[to])continue;
            ++lc[to];
            f[to]+=f[x];
            q.push({(f[to]+(double)G[to].size())/lc[to],to});
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dijkstra(n);
    printf("%.6f\n",f[1]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值