Advertisement
Dmaxiya

星际旅行 参考代码

Mar 5th, 2025
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 1000 + 100;
  6. int n, m, q, u, v, x, y;
  7. double ans;
  8. bool vis[maxn];
  9. int dis[maxn];
  10. int cnt[maxn][maxn];
  11. vector<int> G[maxn];
  12. queue<int> que;
  13.  
  14. void bfs(int s) {
  15.     memset(vis, 0, sizeof(vis));
  16.     que.push(s);
  17.     dis[s] = 0;
  18.     vis[s] = true;
  19.  
  20.     while (!que.empty()) {
  21.         int tmp = que.front();
  22.         que.pop();
  23.         for (int i = 0; i < G[tmp].size(); ++i) {
  24.             int pos = G[tmp][i];
  25.             if (!vis[pos]) {
  26.                 vis[pos] = true;
  27.                 dis[pos] = dis[tmp] + 1;
  28.                 que.push(pos);
  29.             }
  30.         }
  31.     }
  32.  
  33.     for (int i = 1; i <= n; ++i) {
  34.         if (!vis[i]) {
  35.             continue;
  36.         }
  37.         ++cnt[s][dis[i]];
  38.     }
  39.     for (int i = 1; i <= n; ++i) {
  40.         cnt[s][i] += cnt[s][i - 1];
  41.     }
  42. }
  43.  
  44. int main() {
  45. #ifdef ExRoc
  46.     freopen("test.txt", "r", stdin);
  47. #endif // ExRoc
  48.     ios::sync_with_stdio(false);
  49.  
  50.     cin >> n >> m >> q;
  51.     for (int i = 0; i < m; ++i) {
  52.         cin >> u >> v;
  53.         G[u].push_back(v);
  54.         G[v].push_back(u);
  55.     }
  56.     for (int i = 1; i <= n; ++i) {
  57.         bfs(i);
  58.     }
  59.     for (int i = 0; i < q; ++i) {
  60.         cin >> x >> y;
  61.         ans += cnt[x][y];
  62.     }
  63.     printf("%.2lf\n", ans / q);
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement