(15)关键路径.cpp
上传用户:wxj1219
上传日期:2013-01-31
资源大小:6k
文件大小:2k
源码类别:

数据结构

开发平台:

C/C++

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. int NodeCount;
  5. char Node[20];
  6. char ddd;
  7. char Graph[20][20];
  8. char Graphs[20][20];
  9. char RuDu[20];
  10. char Checked[20];
  11. char Value[20];
  12. char cho[20];
  13. char sss;
  14. char Locate(char Char)
  15. {
  16. char Temp;
  17. for(Temp=0;Temp<NodeCount;Temp++)
  18. if(Node[Temp]==Char)
  19. break;
  20. return Temp;
  21. }
  22. void main()
  23. {
  24. char Temp1,Temp2;
  25. int Temp3,Temp4;
  26. int max,current;
  27. char Condition=1;
  28. char min,mintemp,mintemp1;
  29. clrscr();
  30. for(Temp2=0;Temp2<20;Temp2++)
  31. for(Temp1=0;Temp1<20;Temp1++)
  32. Graph[Temp2][Temp1]=0;
  33. for(Temp1=0;Temp1<20;Temp1++)
  34. {
  35. Checked[Temp1]=0;
  36. Value[Temp1]=0;
  37. cho[Temp1]=255;
  38. }
  39. cout<<"Input the node count:";
  40. cin>>NodeCount;
  41. cout<<"Input the node:";
  42. for(Temp1=0;Temp1<NodeCount;Temp1++)
  43.   cin>>Node[Temp1];
  44. cout<<"Input the link"<<endl;
  45. Temp1=0;
  46. while(1)
  47. {
  48. cout<<"Input the first node:";
  49. cin>>Temp1;
  50. cout<<"Input the second node:";
  51. cin>>Temp2;
  52. cout<<"Input the value:";
  53. cin>>Temp3;
  54. if(Temp1=='$')
  55. break;
  56. Temp1=Locate(Temp1);
  57. Temp2=Locate(Temp2);
  58. Graph[Temp1][Temp2]=Temp3;
  59. }
  60. for(Temp2=0;Temp2<NodeCount;Temp2++)
  61. {
  62. for(Temp1=0;Temp1<NodeCount;Temp1++)
  63. cout<<(int)Graph[Temp2][Temp1]<<" ";
  64. cout<<endl;
  65. }
  66. for(Temp2=0;Temp2<20;Temp2++)
  67. for(Temp1=0;Temp1<20;Temp1++)
  68. Graphs[Temp2][Temp1]=Graph[Temp2][Temp1];
  69. while(Condition)
  70. {
  71. for(Temp1=0;Temp1<NodeCount;Temp1++)
  72. RuDu[Temp1]=0;
  73. for(Temp2=0;Temp2<NodeCount;Temp2++)
  74. {
  75. Temp3=0;
  76. for(Temp1=0;Temp1<NodeCount;Temp1++)
  77. if(Graph[Temp1][Temp2])
  78. Temp3++;
  79. RuDu[Temp2]=Temp3;
  80. }
  81. Condition=0;
  82. for(Temp1=0;Temp1<NodeCount;Temp1++)
  83. if((!RuDu[Temp1])&&(!Checked[Temp1]))
  84. {
  85. for(Temp2=0;Temp2<NodeCount;Temp2++)
  86. Graph[Temp1][Temp2]=0;
  87. Checked[Temp1]=1;
  88. max=0;
  89. for(Temp4=0;Temp4<NodeCount;Temp4++)
  90. if((Graphs[Temp4][Temp1]>0)&&(max<Value[Temp4]+Graphs[Temp4][Temp1]))
  91. {
  92. max=Value[Temp4]+Graphs[Temp4][Temp1];
  93. current=Temp4;
  94. }
  95. cout<<Node[Temp1]<<max<<endl;
  96. Value[Temp1]=max;
  97. sss=current;
  98. ddd=Temp1;
  99. Condition=1;
  100. }
  101. }
  102. cout<<endl;
  103. cout<<Node[0]<<"->";
  104. Temp4=0;
  105. n1:for(Temp1=0;Temp1<NodeCount;Temp1++)
  106. {
  107. if((Graphs[Temp1][sss]==Value[sss]-Value[Temp1])&&(sss!=0))
  108. {
  109. cho[Temp4]=sss;
  110. sss=Temp1;
  111. Temp4++;
  112. goto n1;
  113. }
  114. }
  115. for(Temp3=Temp4-1;Temp3>=0;Temp3--)
  116. cout<<Node[cho[Temp3]]<<"->";
  117. cout<<Node[ddd];
  118. }