[gym101620G] Gambling Guide

Time limit: 3 s
Memory limit: 512 MiB

A railroad network in a nearby country consists of nn cities numbered 11 through nn , and mm 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 aa , the
machine dispenses a single one-way ticket from aa 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 11 (where she lives) to city nn (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 nn.
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 nn and m(1n,m300000)m (1 ≤ n , m ≤ 300000) — 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 10610^{−6} .

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]爲從xxnn的期望步數

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

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]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]

f[x]=du[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[y]<f[x]f[y]<f[x]都可以更新f[x]f[x]
接着就是直接最短路型dp即可。
記住要倒着dp,否則期望會有後效性。
lc[x]lc[x]表示在這個狀態下達到f[x]f[x]最小的f[y]<f[x]f[y]<f[x]yy的數目

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章