当前位置:   article > 正文

POJ 1125(用floyd算法的 + 贪心)_floyd算法采用贪心

floyd算法采用贪心
  1. /*
  2. 先求出每对顶点的最短距离,再取出每组(一点到全部点的最短距离为一级) 的最大距离,再在这些最大距离中,取出最小距离,即可;
  3. */
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstring>
  7. #include <algorithm>
  8. using namespace std;
  9. const int INF = 0xffffff;
  10. int map[105][105];
  11. void init(int count){
  12. for(int i=1; i<=count; ++i){
  13. for(int j=1; j<=count; ++j){
  14. if(i!=j){
  15. map[i][j] = INF;
  16. }else{
  17. map[i][j] = 0;
  18. }
  19. }
  20. }
  21. }
  22. void floyd(int n){
  23. for(int k=1; k<=n; ++k){
  24. for(int i=1; i<=n; ++i){
  25. for(int j=1; j<=n; ++j){
  26. map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
  27. }
  28. }
  29. }
  30. }
  31. int main(){
  32. int count,path,y,temp;
  33. while(1){
  34. scanf("%d",&count);
  35. if(count == 0) break;
  36. init(count);
  37. for(int i=1; i<=count; ++i){
  38. cin>>path;
  39. for(int j=1; j<=path; ++j){
  40. cin>>y>>temp;
  41. map[i][y] = temp;
  42. }
  43. }
  44. floyd(count);
  45. int res = INF, loc;
  46. for(int i=1; i<=count; ++i){
  47. temp = -(1<<30);
  48. for(int j=1; j<=count; ++j){
  49. temp = max(temp, map[i][j]);
  50. }
  51. if(res > temp){
  52. loc = i;
  53. res = temp;
  54. }
  55. }
  56. if(res == INF){
  57. printf("disjoint\n");
  58. continue;
  59. }
  60. printf("%d %d\n",loc, res);
  61. }
  62. return 0;
  63. }

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

闽ICP备14008679号