Time limit: 3 s
Memory limit: 512 MiB
A railroad network in a nearby country consists of cities numbered through , and 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 , the
machine dispenses a single one-way ticket from 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 (where she lives) to city (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 .
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 and — 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 .
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點的期望買票次數最小是多少。
題解:
設爲從到的期望步數
。
然後所有都可以更新
接着就是直接最短路型dp即可。
記住要倒着dp,否則期望會有後效性。
表示在這個狀態下達到最小的的的數目
#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;
}