当前位置:   article > 正文

社交网络图中结点的“重要性“计算(Dijkstra + SPFA + Floyd + 模板)

7-3 社交网络图中结点的“重要性”计算

题目链接:


题目大意:

求一个点到其他所有点的最短距离和,保证图连通。


解题过程:

刚开始用 Floyd 水过的,后来用换了几种方法,不错的模板题,Floyd 的时候,要用 vector 存边,否则超内存。


题目分析


AC代码(Dijkstra + SPFA)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 11234, INF = 0x3f3f3f3f;
  4. vector<int> edges[MAX];
  5. int dist[MAX], book[MAX];
  6. void spfa(int s) {
  7. memset(dist, INF, sizeof(dist));
  8. memset(book, 0, sizeof(book));
  9. queue<int> q;
  10. q.push(s);
  11. book[s] = 1;
  12. dist[s] = 0;
  13. while (!q.empty()) {
  14. int u = q.front();
  15. for (int i = 0; i < edges[u].size(); i++) {
  16. int v = edges[u][i];
  17. if (dist[v] > dist[u] + 1) {
  18. dist[v] = dist[u] + 1;
  19. if (!book[v]) {
  20. q.push(v);
  21. book[v] = 1;
  22. }
  23. }
  24. }
  25. q.pop();
  26. book[u] = 0;
  27. }
  28. }
  29. void dijkstra(int s) {
  30. memset(dist, INF, sizeof(dist));
  31. priority_queue<pair<int, int> > q;
  32. dist[s] = 0;
  33. q.push(make_pair(-dist[s], s));
  34. while (!q.empty()) {
  35. int u = q.top().second;
  36. q.pop();
  37. for (int i = 0; i < edges[u].size(); i++) {
  38. int v = edges[u][i];
  39. if (dist[v] > dist[u] + 1) {
  40. dist[v] = dist[u] + 1;
  41. q.push(make_pair(-dist[v], v));
  42. }
  43. }
  44. }
  45. }
  46. int main() {
  47. int n, m;
  48. scanf("%d %d", &n, &m);
  49. while (m--) {
  50. int u, v;
  51. scanf("%d %d", &u, &v);
  52. edges[u].push_back(v);
  53. edges[v].push_back(u);
  54. }
  55. int k;
  56. scanf("%d", &k);
  57. while (k--) {
  58. int s;
  59. scanf("%d", &s);
  60. dijkstra(s);
  61. int sum = 0;
  62. for (int i = 1; i <= n; i++) {
  63. if (i == s)
  64. continue;
  65. sum += dist[i];
  66. }
  67. printf("Cc(%d)=%.2f\n", s, (n-1.0)/sum);
  68. }
  69. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

AC代码(Floyd):

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int INF = 0x3f3f3f3f, MAX = 10001;
  4. int main()
  5. {
  6. vector<int>edge[MAX];
  7. int n, m;
  8. scanf("%d %d", &n, &m);
  9. for (int i = 0; i <= n; i++) {
  10. for (int j = 0; j <= n; j++) {
  11. edge[i].push_back(INF);
  12. }
  13. }
  14. for (int i = 1; i <= n; i++) {
  15. edge[i][i] = 0;
  16. }
  17. for (int i = 0; i < m; i++) {
  18. int u, v;
  19. scanf("%d %d", &u, &v);
  20. edge[u][v] = edge[v][u] = 1;
  21. }
  22. for (int k = 1; k <= n; k++) {
  23. for (int i = 1; i <= n; i++) {
  24. for (int j = 1; j <= n; j++) {
  25. if (edge[i][j] > edge[i][k] + edge[k][j])
  26. edge[i][j] = edge[i][k] + edge[k][j];
  27. }
  28. }
  29. }
  30. int k;
  31. scanf("%d", &k);
  32. while (k--) {
  33. int c;
  34. scanf("%d", &c);
  35. double sum = 0;
  36. for (int i = 1; i <= n; i++) {
  37. if (i == c)
  38. continue;
  39. sum += edge[c][i];
  40. }
  41. printf("Cc(%d)=%.2f\n", c, (n-1)/sum);
  42. }
  43. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

转载于:https://www.cnblogs.com/ACMFish/p/7222852.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/677436
推荐阅读
相关标签
  

闽ICP备14008679号