• 14 图论


    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. int const N = 510;
    6. int a, n;
    7. int g[N][N];
    8. bool vis[N];
    9. int fat[N];
    10. struct things
    11. {
    12. int x;
    13. int y;
    14. int money;
    15. };
    16. vectorv;
    17. bool compare(things t1, things t2)
    18. {
    19. return t1.money < t2.money;
    20. }
    21. int father(int x)
    22. {
    23. return fat[x] == x ? x : father(fat[x]);
    24. }
    25. int main()
    26. {
    27. cin >> a >>n;
    28. for (int i = 1;i <= n;i++)
    29. {
    30. fat[i] = i;
    31. for (int j = 1;j <= n;j++)
    32. {
    33. cin >> g[i][j];
    34. if (g[i][j] == 0)
    35. {
    36. g[i][j] = a;
    37. }
    38. }
    39. }
    40. things t;
    41. for (int i = 1;i < n;i++)
    42. {
    43. for (int j = i + 1;j <= n;j++)
    44. {
    45. t.x = i;
    46. t.y = j;
    47. t.money = g[i][j];
    48. v.push_back(t);
    49. }
    50. }
    51. sort(v.begin(), v.end(), compare);
    52. int cnt = 1;
    53. long long ans = a;
    54. for (vector::iterator it = v.begin();it != v.end();it++)
    55. {
    56. if (father((*it).x) != father((*it).y))
    57. {
    58. int x = father((*it).x), y = father((*it).y);
    59. fat[x] = fat[y];
    60. cnt++;
    61. if ((*it).money < a)
    62. {
    63. ans += (*it).money;
    64. }
    65. else
    66. {
    67. ans += a;
    68. }
    69. }
    70. if (cnt == n)break;
    71. }
    72. cout << ans << endl;
    73. return 0;
    74. }

     

    1. #include
    2. #include
    3. using namespace std;
    4. int const N = 1010;
    5. int fa[N];
    6. int find(int x)
    7. {
    8. if (fa[x] != x)
    9. {
    10. fa[x] = find(fa[x]);
    11. }
    12. return fa[x];
    13. }
    14. void unity(int x, int y)
    15. {
    16. int r1 = find(x);
    17. int r2 = find(y);
    18. fa[r1] = r2;
    19. }
    20. int main()
    21. {
    22. int n, k;
    23. cin >> n;
    24. while (n != 0)
    25. {
    26. cin >> k;
    27. for (int i = 1;i <= n;i++)
    28. {
    29. fa[i] = i;
    30. }
    31. for (int i = 1;i <= k;i++)
    32. {
    33. int u, v;
    34. cin >> u >> v;
    35. unity(u, v);
    36. }
    37. int ans = -1;
    38. for (int i = 1;i <= n;i++)
    39. {
    40. if (fa[i] == i)
    41. {
    42. ans++;
    43. }
    44. }
    45. cout << ans << endl;
    46. cin >> n;
    47. }
    48. return 0;
    49. }

     

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. int n, m, k;
    6. int fa[1010];
    7. int find(int x)
    8. {
    9. if (fa[x] != x)
    10. {
    11. fa[x] = find(fa[x]);
    12. }
    13. return fa[x];
    14. }
    15. void unity(int x, int y)
    16. {
    17. int r1 = find(x);
    18. int r2 = find(y);
    19. fa[r1] = r2;
    20. }
    21. struct lt
    22. {
    23. int x;
    24. int y;
    25. int dj;
    26. };
    27. bool cmp(lt l1, lt l2)
    28. {
    29. return l1.dj < l2.dj;
    30. }
    31. int main()
    32. {
    33. vectorv;
    34. lt l;
    35. cin >> n >> m >> k;
    36. for (int i = 1;i <= n;i++)
    37. {
    38. fa[i] = i;
    39. }
    40. for (int i = 1;i <= m;i++)
    41. {
    42. cin >> l.x >> l.y >> l.dj;
    43. v.push_back(l);
    44. }
    45. sort(v.begin(), v.end(), cmp);
    46. int cnt = n;
    47. long long ans = 0;
    48. int length = v.size();
    49. //cout << endl << endl << endl;
    50. for (int i = 0;i < length;i++)
    51. {
    52. //cout << v[i].x<<" " << v[i].y<<" " << v[i].dj << endl;
    53. if (cnt == k)
    54. {
    55. cout << ans << endl;
    56. return 0;
    57. }
    58. if (find(v[i].x) != find(v[i].y))
    59. {
    60. unity(find(v[i].x), find(v[i].y));
    61. ans += v[i].dj;
    62. cnt--;
    63. }
    64. if (cnt == k)
    65. {
    66. cout << ans << endl;
    67. return 0;
    68. }
    69. }
    70. cout << "No Answer" << endl;
    71. return 0;
    72. }

     

     

     

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. int n, m, s, t;
    6. int const N = 101000;
    7. int fa[N];
    8. struct lu
    9. {
    10. int x;
    11. int y;
    12. int yongji;
    13. };
    14. bool cmp(lu l1, lu l2)
    15. {
    16. return l1.yongji < l2.yongji;
    17. }
    18. int find(int x)
    19. {
    20. if (fa[x] != x)
    21. {
    22. fa[x] = find(fa[x]);
    23. }
    24. return fa[x];
    25. }
    26. void unity(int x, int y)
    27. {
    28. int r1 = find(x);
    29. int r2 = find(y);
    30. fa[r1] = r2;
    31. }
    32. int main()
    33. {
    34. cin >> n >> m >> s >> t;
    35. vectorv;
    36. lu l;
    37. for (int i = 1;i <= n;i++)
    38. {
    39. fa[i] = i;
    40. }
    41. for (int i = 1;i <= m;i++)
    42. {
    43. cin >> l.x >> l.y >> l.yongji;
    44. v.push_back(l);
    45. }
    46. sort(v.begin(), v.end(), cmp);
    47. long long ans = 0;
    48. for (vector::iterator it = v.begin();it != v.end();it++)
    49. {
    50. int x = (*it).x;
    51. int y = (*it).y;
    52. if (find(x) != find(y))
    53. {
    54. unity(find(x), find(y));
    55. }
    56. if (find(s) == find(t))
    57. {
    58. cout <<(*it).yongji << endl;
    59. return 0;
    60. }
    61. }
    62. return 0;
    63. }

    1. #include
    2. #include
    3. using namespace std;
    4. int const N = 110;
    5. struct wl
    6. {
    7. int x;
    8. int y;
    9. int jl;
    10. };
    11. int n;
    12. int fa[N];
    13. int g[N][N];
    14. wl w[N*N];
    15. bool cmp(wl w1, wl w2)
    16. {
    17. return w1.jl < w2.jl;
    18. }
    19. int find(int x)
    20. {
    21. if (fa[x] != x)
    22. {
    23. fa[x] = find(fa[x]);
    24. }
    25. return fa[x];
    26. }
    27. void unity(int x, int y)
    28. {
    29. int r1 = find(x);
    30. int r2 = find(y);
    31. fa[r1] = r2;
    32. }
    33. int main()
    34. {
    35. cin >> n;
    36. for (int i = 1;i <= n;i++)
    37. {
    38. fa[i] = i;
    39. for (int j = 1;j <= n;j++)
    40. {
    41. cin >> g[i][j];
    42. }
    43. }
    44. int flag = 1;
    45. for (int i = 1;i < n;i++)
    46. {
    47. for (int j = i + 1;j <= n;j++)
    48. {
    49. w[flag].x = i;
    50. w[flag].y = j;
    51. w[flag].jl = g[i][j];
    52. flag++;
    53. }
    54. }
    55. sort(w + 1, w + flag, cmp);
    56. long long ans = 0;
    57. int cnt = 1;
    58. for (int i = 1;i <= flag;i++)
    59. {
    60. int x = find(w[i].x);
    61. int y = find(w[i].y);
    62. if (x != y)
    63. {
    64. cnt++;
    65. ans += w[i].jl;
    66. unity(x, y);
    67. }
    68. if (cnt == n)
    69. {
    70. cout << ans << endl;
    71. return 0;
    72. }
    73. }
    74. return 0;
    75. }

     

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. int n, m, s;
    8. int const N = 100010;
    9. int dis[N];
    10. bool vis[N];
    11. struct edge
    12. {
    13. int u;
    14. int v;
    15. int w;
    16. };
    17. struct node
    18. {
    19. int number;
    20. int dis;
    21. bool operator < (const node& x)const { return x.dis < dis; }
    22. };
    23. vectorvt[N];
    24. priority_queueq;
    25. void dijkstra()
    26. {
    27. memset(dis, 0x3f, sizeof(dis));
    28. dis[s] = 0;
    29. q.push({ s,0 });
    30. while (!q.empty())
    31. {
    32. int x = q.top().number;
    33. q.pop();
    34. if (vis[x])continue;
    35. vis[x] = true;
    36. for (vector::iterator it = vt[x].begin();it != vt[x].end();it++)
    37. {
    38. int p = (*it).v;
    39. if (vis[p])continue;
    40. if (dis[p] > dis[x] + (*it).w)
    41. {
    42. dis[p] = dis[x] + (*it).w;
    43. q.push({ p,dis[x] + (*it).w });
    44. }
    45. }
    46. }
    47. }
    48. int main()
    49. {
    50. cin >> n >> m >> s;
    51. edge e;
    52. for (int i = 1;i <= m;i++)
    53. {
    54. cin >> e.u >> e.v >> e.w;
    55. vt[e.u].push_back(e);
    56. }
    57. dijkstra();
    58. for (int i = 1;i <= n;i++)
    59. {
    60. cout << dis[i] << " ";
    61. }
    62. return 0;
    63. }

     

     

  • 相关阅读:
    【Quark RISC-V】流水线CPU设计(2)理想流水线
    怎么保护苹果手机移动应用程序ipa中文件安全?
    mysql8.0.34部署单节点与集群-AB复制(IO与SQL线程不同步解决方法)
    一文教你处理SpringBoot统一返回格式
    关于LWIP的一点记录(三)
    海外IP代理科普——API代理是什么?怎么用?
    注意:Spring Boot 2.7开始spring.factories不推荐使用了,接下来这么玩...
    jira操作流程
    树莓派在Raspbian系统(Bookworm)中无法获取RJ45网口eth0或end0的IP地址(没有IPv4的地址无法操作)
    linux Mysql 8.0.16 安装搭建
  • 原文地址:https://blog.csdn.net/weixin_72770922/article/details/136122137