master_lam

Ford-Bellman

Nov 13th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include<process.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. #define N 100
  6. #define VOCUC 32767
  7.  
  8. typedef struct
  9. {
  10. int n;
  11. int a[N][N];
  12. }GRAPH;
  13.  
  14.  
  15. GRAPH g;
  16. int previous[N+1][N]; // luu lai duong di ngan nhat
  17. int mincost[N+1][N]; // do dai duong di tu i den j
  18. int nhan[N]; //nhan kiem tra tinh lien thong
  19. int soTPLT;
  20. int step;
  21.  
  22. int x;
  23.  
  24.  
  25. void doc(GRAPH &g)
  26. {
  27. FILE *f;
  28. f=fopen("D:\\BELLMAN.txt","rt");
  29. if(f==NULL)
  30. {
  31. printf("Khong ton tai File BELLMAN \n");
  32. exit(0);
  33. }
  34. fscanf(f,"%d",&g.n);
  35. for(int i=0;i<g.n;i++)
  36. {
  37. for(int j=0;j<g.n;j++)
  38. {
  39. fscanf(f,"%d",&g.a[i][j]);
  40. }
  41. }
  42.  
  43. fclose(f);
  44. }
  45.  
  46. int kiemTRaVH(GRAPH g)// kiem tra tinh hop le cua do thi
  47. {
  48. for(int i=0;i<g.n;i++)
  49. if(g.a[i][i]!=0)
  50. return 0;
  51. return 1;
  52. }
  53.  
  54. int kt_huong(GRAPH g)
  55. {
  56. for(int i=0;i<g.n;i++)
  57. {
  58. for(int j=0;j<g.n;j++)
  59. if(g.a[i][j]!=g.a[j][i])
  60. return 0;//la do thi co huong
  61. }
  62. return 1;//la do thi vo huong
  63. }
  64.  
  65. void viSit(GRAPH g,int i,int labels)
  66. {
  67. nhan[i]=labels;
  68. for(int j=0;j<g.n;j++)
  69. if(nhan[j]==0 && (g.a[i][j] != 0 || g.a[j][i]!=0))
  70. viSit(g,j,labels);
  71. }
  72.  
  73. int xetLienThong(GRAPH g)
  74. {
  75. for(int i=0; i<g.n; i++)
  76. nhan[i] = 0;
  77. soTPLT=0;
  78. for(i=0; i<g.n; i++)
  79. if(nhan[i] == 0)
  80. {
  81. soTPLT++;
  82. viSit(g,i,soTPLT);
  83. }
  84. if(soTPLT>1)
  85. return 0;
  86. return 1;
  87. }
  88.  
  89. /*khoi tao cac thong so cho thuat toan*/
  90. void Init(GRAPH g,int x)
  91. {
  92. step =0;
  93. for(int i=0;i<g.n;i++)
  94. {
  95. mincost[step][i]=VOCUC; //dung 32767 cho gia tri vo cuc
  96. previous[step][i]=i;
  97. }
  98. mincost[step][x]=0;
  99. }
  100.  
  101. /* Thuat toan Bellman */
  102. void Bellman(GRAPH g,int x)
  103. {
  104. Init(g,x);
  105. int i;
  106. for(step=1;step<=g.n;step++)
  107. {
  108. for(int t=0;t<g.n;t++)
  109. {
  110. mincost[step][t]=mincost[step-1][t];
  111. previous[step][t]=previous[step-1][t];
  112. }
  113. for(i=0;i<g.n;i++)
  114. {
  115. //tim cac dinh j co duong noi tu j den i
  116. //va chi phi buoc step-1 cua j khac VOCUC
  117. for(int j=0;j<g.n;j++)
  118. {
  119. if(g.a[i][j]!=0 && mincost[step-1][i]!=VOCUC)
  120. {
  121. //cap nhat lai neu chi phi buoc step cua i la VOCUC
  122. //hoc chi phi di qua j: mincost[step-1][j]+ a[j][i] toi uu hon
  123. if(mincost[step][j]==VOCUC || mincost[step][j]>mincost[step-1][i]+g.a[i][j])
  124. {
  125. mincost[step][j]=mincost[step-1][i]+g.a[i][j];
  126. previous[step][j]=i;
  127.  
  128. }
  129. }
  130.  
  131. }
  132. }
  133. //so sanh mincost[step] voi mincost[step-1] neu bang nhau thi ket thuc
  134. int bSame = true;
  135. for( i=0;i<g.n;i++)
  136. if(mincost[step][i]!=mincost[step-1][i])
  137. {
  138. bSame=false;
  139. break;
  140. }
  141. if(bSame)
  142. break;
  143. }
  144.  
  145.  
  146. }
  147.  
  148. void xuat(GRAPH g,int x)
  149. {
  150. int z=0;
  151. int k=x,s;
  152. int t[N];
  153. if(kiemTRaVH(g)==0)
  154. {
  155. printf("do thi khong hop le\n");
  156. exit(0);
  157. }
  158. if(kt_huong(g)==1)
  159. printf("Day la do thi vo huong\n");
  160. else
  161. printf("day la do thi co huong\n");
  162. if(step==g.n+1)
  163. printf("do thi co chu trinh am\n");
  164. else
  165. {
  166.  
  167. for( int i=0;i<g.n;i++)
  168. {
  169. if(xetLienThong(g)==0)
  170. {
  171. printf("do thi khong lien thong");
  172. break;
  173. }
  174. if(previous[step-1][i]==i || x==i ||previous[step-1][i]==VOCUC)
  175. printf("tu dinh %d->%d khong co duong di\n",x,i);
  176. else
  177. {
  178. printf("tu dinh %d->%d co do dai %d ",x,i,mincost[step-1][i]);
  179. s=i;
  180. t[0]=i;
  181. z=1;
  182. do
  183.  
  184. {
  185. t[z++]=previous[step][s];
  186. s=previous[step][s];
  187. }while(s!=x);
  188.  
  189. printf("duong di: ");
  190. for(int j=z-1;j>0;j--)
  191. printf("%d->",t[j]);
  192. printf("%d",t[0]);
  193. printf("\n");
  194. }
  195. }
  196.  
  197. }
  198. }
  199.  
  200. void xuatBellman(GRAPH g,int x)
  201. {
  202. int z=0;
  203. int k=x,s;
  204. int t[N];
  205. FILE *f;
  206. f=fopen("D:\\BELLMAN_out.txt","wt");
  207. if(f==NULL)
  208. {
  209. fprintf(f,"khong ton tai file");
  210. exit(0);
  211. }
  212. fprintf(f,"Duong di ngan nhat bang thuat toan BELLMAN\n\n");
  213. if(kiemTRaVH(g)==0)
  214. {
  215. fprintf(f,"do thi khong hop le\n");
  216. exit(0);
  217. }
  218. if(kt_huong(g)==1)
  219. fprintf(f,"Day la do thi vo huong\n");
  220. else
  221. fprintf(f,"day la do thi co huong\n");
  222. if(step==g.n+1)
  223. fprintf(f,"do thi co chu trinh am\n");
  224. else
  225. {
  226.  
  227. for( int i=0;i<g.n;i++)
  228. {
  229. if(xetLienThong(g)==0)
  230. {
  231. fprintf(f,"do thi khong lien thong => khong tim duoc duong di");
  232. break;
  233. }
  234. if(previous[step-1][i]==i || x==i||previous[step-1][i]==VOCUC)
  235. fprintf(f,"tu dinh %d->%d khong co duong di\n",x,i);
  236. else
  237. {
  238. fprintf(f,"tu dinh %d->%d co do dai %d ",x,i,mincost[step-1][i]);
  239. s=i;
  240. t[0]=i;
  241. z=1;
  242. do
  243.  
  244. {
  245. t[z++]=previous[step][s];
  246. s=previous[step][s];
  247. }while(s!=x);
  248.  
  249. fprintf(f,"duong di: ");
  250. for(int j=z-1;j>0;j--)
  251. fprintf(f,"%d->",t[j]);
  252. fprintf(f,"%d",t[0]);
  253. fprintf(f,"\n");
  254. }
  255. }
  256.  
  257. }
  258. fclose(f);
  259.  
  260. }
  261.  
  262. void main()
  263. {
  264. int x;
  265. doc(g);
  266. printf("nhap x: ");
  267. scanf("%d",&x);
  268. Bellman(g,x);
  269. xuat(g,x);
  270. xuatBellman(g,x);
  271. }
  272.  
Advertisement
Add Comment
Please, Sign In to add comment