zoukankan      html  css  js  c++  java
  • hdu 1217 dijkstra

    1. #include <stdio.h>
    2. #include <iostream>
    3. #include <map>
    4. #include <string>
    5. using namespace std;
    6. #define INF 0xfffff
    7. #define MAX 1100
    8. float dist[MAX], path[MAX][MAX];
    9. bool sign[MAX];
    10. /* 注意相应权值不能为负,且时间复杂度较高 */
    11. /*算法步骤如下:
    12. 1. 初始时令 S={V0},T={其余顶点},T中顶点对应的距离值
    13. 若存在<V0,Vi>,d(V0,Vi)为<V0,Vi>弧上的权值
    14. 若不存在<V0,Vi>,d(V0,Vi)为∞
    15. 2. 从T中选取一个其距离值为最小的顶点W且不在S中,加入S
    16. 3. 对其余T中顶点的距离值进行修改:若加进W作中间顶点,从V0到Vi的距离值缩短,则修改此距离值
    17. 重复上述步骤2、3,直到S中包含所有顶点,即W=Vi为止
    18. */
    19. void initialize(int n) //初始化
    20. {
    21. for(int i=1; i<=n; i++)
    22. {
    23. {
    24. //pre[i] = 0;
    25. dist[i] = 0; //将距离开始全变为最大
    26. sign[i] = false;
    27. }
    28. for(int j=1; j<=n; j++)
    29. if(i == j) path[i][j] = 1.0; //图初始
    30. else path[i][j] = 0;
    31. }
    32. }
    33. bool dijkstra(int n, int source )
    34. {
    35. for(int i=1; i<=n; i++)
    36. {
    37. dist[i] = path[source][i]; //将与源点有关的点的距离加入dist
    38. sign[i] = false;
    39. //if(dist[i] == INF) //确定有关系的点的前驱,无则为0
    40. //pre[i] = 0;
    41. //else
    42. // pre[i] = source;
    43. }
    44. dist[source] = 1.0; //源点自身长度为0
    45. sign[source] = 1;
    46. /*
    47. 依次将未放入sign集合的结点中,取dist[]最小值的结点,放入结合sign中
    48. 一旦sign包含了所有n中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度
    49. */
    50. for(int i=2; i<=n; i++)
    51. {
    52. //int min = INF;
    53. int current = source;
    54. for(int j=1; j<=n; j++) //找出当前未使用点j的dist[j]的最小值
    55. {
    56. if( (!sign[j]) && dist[j] * path[current][j] > dist[j] )
    57. {current = j; /*min = dist[j];*/}
    58. }
    59. sign[current] = true; //表示当前点最短距离已经找到
    60. if( dist[source] > 1.0) return true;
    61. for(int j=1; j<=n; j++) //更新当前点到未找到点的距离
    62. //if( (!sign[j]) && path[current][j] < INF)
    63. {
    64. int newdist = dist[current] * path[current][j];
    65. if(newdist > dist[j] )
    66. {dist[j] = newdist; /*pre[j] = current;*/}
    67. }
    68. }return false;
    69. }
    70. /*void search_path(int n, int start, int end)
    71. {
    72. int road[MAX];
    73. int total = 1;
    74. road[total++] = end; //从后向前查找
    75. int current = pre[end]; //路径存在pre中
    76. while( current != start) //递归查找,类似并查集
    77. {
    78. road[total++] = current;
    79. current = pre[current];
    80. }
    81. road[total] = start; //最后的开始点存入
    82. for(int i=total; i>=1; i--) //输出
    83. {
    84. if( i!=1)
    85. printf("%d ->", road[i]);
    86. else
    87. printf("%d ", road[i]);
    88. }
    89. }*/
    90. void input(int line)
    91. {
    92. map<string, int>list;
    93. for(int i=1; i<=line; i++)
    94. {
    95. string temp; cin >> temp;
    96. list[temp] = i;
    97. }
    98. int count;
    99. scanf("%d", &count);
    100. string a, b;
    101. float weight;
    102. for(int i=0; i<count; i++)
    103. {
    104. cin >> a >> weight >> b;
    105. //if(path[list[a]][list[b]] > weight) //有多条路,保存最短的那条
    106. {
    107. path[list[a]][list[b]] = weight;
    108. //path[list[b]][list[a]] = weight; //无向图双向
    109. }
    110. }
    111. }
    112. int main()
    113. {
    114. int n, T=0;
    115. while(~scanf("%d", &n) && n )
    116. {
    117. initialize(n);
    118. input(n);
    119. int flag =1;
    120. for(int i=1; i<=n; i++)
    121. {
    122. if(dijkstra(n, i) )
    123. {printf("Case %d: Yes ", ++T); flag = 0; break;}
    124. }
    125. if(flag) {printf("Case %d: No ", ++T); }
    126. }
    127. return 0;
    128. }





    附件列表

    • 相关阅读:
      类的设计问题
      php数组存在重复的相反元素,去重复
      常用JS验证函数总结
      python常用模块
      re 模块
      logging 模块
      configparser模块
      python 文件处理
      第15章-输入/输出 --- 理解Java的IO流
      第10章-验证框架 --- 验证器类型
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/7f86d92399eb1ec197e56e0b991fe64c.html
    Copyright © 2011-2022 走看看