【CodeForces - 208C 】Police Station(单源最短路条数,起点终点建图,枚举顶点)
題干:
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?mlines 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 4Output
1.000000000000Input
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 4Output
1.714285714286Note
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??.
題目大意:
? ? ? 有n個(gè)城市,編號為1-n,有m條雙向路,現(xiàn)要在一個(gè)點(diǎn)設(shè)立警察局,通過這個(gè)點(diǎn)的路就是安全路,求1到n的每條最短路上平均的安全路的條數(shù),即安全路總可能條數(shù)/總最短路條數(shù)。
題意:
給你n個(gè)點(diǎn)(編號為1到n),m條邊的有向圖(無環(huán),無重邊,每個(gè)點(diǎn)都與其他點(diǎn)連通),現(xiàn)在要在一個(gè)點(diǎn)上建一個(gè)警察局,一條邊,只要有一端與警察局相連,它就是安全的邊,否則它就是不安全的邊,現(xiàn)在問在哪個(gè)點(diǎn)建警察局能使從1到n的每條最短路上平均的安全路的條數(shù),即最大。這里的每條最短路的定義是最短路上有1條邊不一樣就是不一樣的最短路徑。
思路:
根據(jù)這題不同最短路的定義,我們可以知道對于中間2~n-1的節(jié)點(diǎn)來說,如果放置警察局,那么它(設(shè)為i)所能產(chǎn)生安全的道路條數(shù)為從i到1和i到n的最短路徑條數(shù)相乘,結(jié)果是從1到n且經(jīng)過i的最短路徑條數(shù),且對于每條不同的最短路徑來說會產(chǎn)生2條安全邊。所以我們枚舉i來跑遍SPFA就可以了。
思路:
題目比較難理解,吃了英語的虧,首先是安全路總可能條數(shù),在點(diǎn)2-n-1中某個(gè)點(diǎn)x建警察局,則在每條最短路中會產(chǎn)生2條安全路,在x點(diǎn)建立警察局所產(chǎn)生的總條數(shù)就是1到x的路徑*x到n的路徑;總最短路條數(shù)便是1到n的所有最短路條數(shù),于是可以用spfa計(jì)算最短路,dp記錄條數(shù)。另外要注意最短路和條數(shù)要使用long long,否則會載在Test 22,因?yàn)榧僭O(shè)六個(gè)一組形成16個(gè)4叉路,剩下四個(gè)再形成三叉路,則總最短路條數(shù)為2^32*3,大于int范圍。(思路比較難理解,建議邊看代碼邊理解)。
解題報(bào)告:
?
AC代碼:?
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 200 + 5; vector<int> vv[MAX]; ll dis[MAX]; ll ans[MAX]; bool vis[MAX]; int n,m; struct Point {int pos;ll c;Point(){}Point(int pos,ll c):pos(pos),c(c){}bool operator < (const Point & b)const{return c > b.c;} }; void Dijkstra(int st) {for(int i = 1; i<=n; i++) dis[i] = 100000000000;memset(ans,0,sizeof ans);memset(vis,0,sizeof vis);dis[st]=0;ans[st]=1;priority_queue<Point> pq;pq.push(Point(st,0));while(!pq.empty()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;int size = vv[cur.pos].size();for(int i = 0; i<size; i++) {int v = vv[cur.pos][i];if(vis[v]) continue;if(dis[v] == dis[cur.pos] + 1) {ans[v] += ans[cur.pos];}else if(dis[v] > dis[cur.pos] + 1) {ans[v] = ans[cur.pos];dis[v] = dis[cur.pos] + 1;pq.push(Point(v,dis[v])); }}} }int main() {double maxx = -1;int u,v;cin>>n>>m;while(m--) {scanf("%d%d",&u,&v);vv[u].pb(v);vv[v].pb(u);}Dijkstra(1);ll len = dis[n],shortest = ans[n];//以此為基準(zhǔn) // maxx = max(maxx,(ans[n]*ans[1])*2.0 / shortest);加上就錯(cuò)了maxx = 1;for(int i = 2; i<=n-1; i++) {Dijkstra(i);if(dis[1] + dis[n] == len)maxx = max(maxx,((ans[n]*ans[1])*2.0) / (1.0*shortest));}printf("%.12lf\n",maxx);return 0 ;}AC代碼2:(floyd算法亦可跑最短路條數(shù),和下面附的一個(gè)代碼類似,只是更新方式不同,他那個(gè)直接更新成0,然后下面的那個(gè)if中再更新成原值)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 200 + 5; int maze[MAX][MAX]; ll dp[MAX][MAX]; int main() {int n,m; int u,v;cin>>n>>m;memset(maze,INF,sizeof maze);while(m--) {scanf("%d%d",&u,&v);maze[u][v] = maze[v][u] = 1;dp[u][v] = dp[v][u] = 1;} double ans = 1.0;for(int k = 1; k<=n; k++) {for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(maze[i][j] == maze[i][k] + maze[k][j]) {dp[i][j] += dp[i][k] * dp[k][j];}else if(maze[i][j] > maze[i][k] + maze[k][j]) {maze[i][j] = maze[i][k] + maze[k][j];dp[i][j] = dp[i][k] * dp[k][j];}}}}for(int i = 2; i<=n-1; i++) {if(maze[1][i] + maze[i][n] == maze[1][n])ans = max(ans,2.0*(dp[1][i]*dp[i][n])/dp[1][n]);}printf("%.12f",ans);return 0 ;}?
附:
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 208C 】Police Station(单源最短路条数,起点终点建图,枚举顶点)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 特斯拉失宠 中国车企上位!韩国基金押注国
- 下一篇: 坐等升级!微软确认:Windows 10