(16)最短路径.CPP
上传用户:wxj1219
上传日期:2013-01-31
资源大小:6k
文件大小:2k
源码类别:

数据结构

开发平台:

C/C++

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. char path[20];
  5. char maxpath[20];
  6. char max;
  7. int NodeCount;
  8. int maxx=200;
  9. char last;
  10. char Node[20];
  11. char Graph[20][20];
  12. void f(int Current,int count,int depth);
  13. char Locate(char Char)
  14. {
  15. char Temp;
  16. for(Temp=0;Temp<NodeCount;Temp++)
  17. if(Node[Temp]==Char)
  18. break;
  19. return Temp;
  20. }
  21. void main()
  22. {
  23. char Temp1,Temp2;
  24. int Temp3;
  25. char Condition=1;
  26. char min,mintemp,mintemp1;
  27. clrscr();
  28. for(Temp2=0;Temp2<20;Temp2++)
  29. for(Temp1=0;Temp1<20;Temp1++)
  30. Graph[Temp2][Temp1]=0;
  31. cout<<"Input the node count:";
  32. cin>>NodeCount;
  33. cout<<"Input the node:";
  34. for(Temp1=0;Temp1<NodeCount;Temp1++)
  35.   cin>>Node[Temp1];
  36. cout<<"Input the link"<<endl;
  37. Temp1=0;
  38. while(1)
  39. {
  40. cout<<"Input the first node:";
  41. cin>>Temp1;
  42. cout<<"Input the second node:";
  43. cin>>Temp2;
  44. cout<<"Input the value:";
  45. cin>>Temp3;
  46. if(Temp1=='$')
  47. break;
  48. Temp1=Locate(Temp1);
  49. Temp2=Locate(Temp2);
  50. Graph[Temp1][Temp2]=Temp3;
  51. }
  52. cout<<"Input the last node:";
  53. cin>>last;
  54. for(Temp2=0;Temp2<NodeCount;Temp2++)
  55. {
  56. for(Temp1=0;Temp1<NodeCount;Temp1++)
  57. cout<<(int)Graph[Temp2][Temp1]<<" ";
  58. cout<<endl;
  59. }
  60. f(0,0,0);
  61. for(Temp1=0;Temp1<max;Temp1++)
  62. cout<<Node[maxpath[Temp1]]<<"->";
  63. cout<<last<<endl;
  64. cout<<"Min="<<(int)maxx;
  65. }
  66. void f(int Current,int count,int depth)
  67. {
  68. int u;
  69. char s;
  70. int c;
  71. if(Node[Current]==last)
  72. {
  73. if(count<maxx)
  74. {
  75. for(c=0;c<=NodeCount;c++)
  76. maxpath[c]=path[c];
  77. max=depth;
  78. maxx=count;
  79. return;
  80. }
  81. }
  82. for(u=0;u<NodeCount;u++)
  83. {
  84. if(Graph[Current][u]>0)
  85. {
  86. s=Graph[Current][u];
  87. Graph[Current][u]=0;
  88. path[depth]=Current;
  89. f(u,count+s,depth+1);
  90. Graph[Current][u]=s;
  91. }
  92. }
  93. }