• Kruskal,最短路综合应用,一道图论一


    Problem - 1525 (nefu.edu.cn)

    Problem:1525
    Time Limit:1000ms
    Memory Limit:131072K

    Description

    给定一个包含 n  个节点和 m  条边的图,每条边有一个权值。
    你的任务是回答 k 个询问,每个询问包含两个正整数 s  和 t 表示起点和终点,要求寻找从 s 到 t  的一条路径,使得路径上权值最大的一条边权值最小。

    Input

    第一行包含三个整数 n 、m 、k ,分别表示 n 个节点, m  条路径, k  个询问。
    
    接下来 m  行,每行三个整数 u , v  , w, 表示一个由 u 到 v  的长度为 w  的双向边。
    
    再接下来 k  行,每行两个整数 s  , t,表示询问从 s 连接到 t  的所有路径中单边长度最大值的最小值。

    Output

    输出包含 k  行,每一行包含一个整数 p 。p  表示 s  连接到 t 的所有路径中单边长度最大值的最小值。另外,如果 s 到 t  没有路径相连通,输出 -1 即可。

    Sample Input

    8 11 3
    1 2 10
    2 5 50
    3 4 60
    7 5 60
    3 6 30
    1 5 30
    6 7 20
    1 7 70
    2 3 20
    3 5 40
    2 6 90
    1 7
    2 8
    6 2
    

    Sample Output

    30
    -1
    30

    Hint

    对于 100% 的数据 n≤1000,m≤100000,k≤1000,w≤10000000  
    本题可能会有重边。
    为了避免 Special Judge,本题所有的 w 均不相同
    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. using namespace std;
    17. typedef long long LL;
    18. const int N = 1e3 + 5, M = 1e5 + 5;
    19. int n, m, k;
    20. struct edge {
    21. int a, b, w;
    22. }edge[M];
    23. int fa[N],d[N],vis[N];
    24. vectorint, int>>G[N];
    25. int find(int x) {
    26. if (fa[x] == x)
    27. return x;
    28. return fa[x] = find(fa[x]);
    29. }
    30. int cmp(const struct edge& a, const struct edge& b) {
    31. return a.w < b.w;
    32. }
    33. void spfa(int x) {
    34. queue<int>q;
    35. memset(d, 0x3f3f3f3f, sizeof(d));
    36. memset(vis, 0, sizeof(vis));
    37. d[x] = 0;
    38. q.push(x);
    39. int t;
    40. while (!q.empty()) {
    41. t = q.front();
    42. q.pop();
    43. vis[x] = 0;
    44. for (int i = 0,j,w; i < G[t].size(); i++) {
    45. j = G[t][i].first, w = G[t][i].second;
    46. if (d[j] > max(d[t],w)) {
    47. d[j] = max(d[t], w);
    48. if (!vis[j]) {
    49. q.push(j);
    50. vis[j] = 1;
    51. }
    52. }
    53. }
    54. }
    55. }
    56. int main() {
    57. scanf("%d%d%d", &n, &m, &k);
    58. for (int i = 1; i <= m; i++) {
    59. scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].w);
    60. }
    61. for (int i = 1; i <= n; i++) {
    62. fa[i] = i;
    63. }
    64. sort(edge + 1, edge + 1 + m, cmp);
    65. int x, y;
    66. for (int i = 1; i <= m; i++) {
    67. x = find(edge[i].a);
    68. y = find(edge[i].b);
    69. if (y != x) {
    70. G[edge[i].a].push_back({ edge[i].b,edge[i].w });
    71. G[edge[i].b].push_back({ edge[i].a,edge[i].w });
    72. fa[x] = y;
    73. }
    74. }
    75. for (int i = 1; i <= k; i++) {
    76. scanf("%d%d", &y, &x);
    77. spfa(y);
    78. if (d[x] == 0x3f3f3f3f)
    79. printf("-1\n");
    80. else {
    81. printf("%d\n", d[x]);
    82. }
    83. }
    84. return 0;
    85. }

  • 相关阅读:
    分布式数据库Schema 变更 in F1 & TiDB
    探讨Socks5代理技术的原理及其在不同领域的应用
    Azure微软云
    面对新的测试框架不要慌看这里~
    性能优化:Netty连接参数优化
    论文阅读笔记 | 三维目标检测——PointNet
    mybatisPlus
    【实战分享】使用 Go 重构流式日志网关
    【C++】模板初阶,了解函数模板和类模板的使用&&一些可能出现的问题
    kubernetes-nvidia-plugin设计解读
  • 原文地址:https://blog.csdn.net/Landing_on_Mars/article/details/132865721