Codeforces 208C Police Station (BFS+DP计数)

The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time.

All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one).

The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous.

Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value.

Input

The first input line contains two integers n and m (2 ≤ n ≤ 100, ) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space.

It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads.

Output

Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6.

Examples

Input

4 4
1 2
2 4
1 3
3 4

Output

1.000000000000

Input

11 14
1 2
1 3
2 4
3 4
4 5
4 6
5 11
6 11
1 8
8 9
9 7
11 7
1 10
10 4

Output

1.714285714286

Note

In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make .

In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal .

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

typedef __int64 LL;

const int INF=0x3f3f3f;
const int MAX=105;
int n,m;

vector<int> gr[MAX];
LL dis[MAX],dp[MAX];
/*
题目大意:n个点,两个中心,
在其中选一个作为警局,
经过此的最短路都贡献为2(1和n除外)
然后算个比重。

核心就是如果选j点作为警局,
如何把j到n和j到1的最短路数量求出来。

这题计方法如果直接用BFS和计数数组直接++
的话会超时(虽然方法是对的),直接引入计数DP
即可,在到达j点时,把上一个点的计数累加到j点(如果j点符合最短路要求)。

还有一个坑点是,枚举的j点,其最短路长度之和必须是1到n的最短路长度。
*/
void BFS(int s){
	for(int i=1;i<=n;i++){
		dis[i]=INF;
		dp[i]=0;
	}
	dis[s]=0,dp[s]=1;
	queue<int> Q;
	Q.push(s);
	while(!Q.empty()){
		int u=Q.front();
		Q.pop();
		for(int i=0;i<gr[u].size();i++){
			int v=gr[u][i];
			if(dis[v]>dis[u]+1){
				dis[v]=dis[u]+1;
				dp[v]=dp[u];
				Q.push(v);
			}
			else if(dis[v]==dis[u]+1)  dp[v]+=dp[u]; ///计数dp的思想。。。
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	int u,v;
	while(m--){
		scanf("%d%d",&u,&v);
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	BFS(1);
	LL len=dis[n],tol=dp[n];
	double ans=1.0;
	for(int i=2;i<=n-1;i++){
		BFS(i);
		if((dis[1]+dis[n])==len)    ans=max(ans,(2.0*dp[1]*dp[n])/(1.0*tol));
	}
	printf("%.12lf\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81369328