Advertisement
Guest User

tour.c

a guest
Dec 3rd, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 20
  5. int adjacency[SIZE][SIZE];
  6. int best_cost =1000000;
  7. int *BestList;
  8. int permutations = 0;
  9. void print_tour(int[],int,int);
  10. main()
  11. {
  12.     int best_cost;
  13.     void print_tour();
  14.     void permute_list();
  15.     int i,j,weight,num;
  16.     int *NodeList;
  17.     char c;
  18.     int cost;
  19.     int thisCost;
  20.  
  21.     for( i = 0; i < SIZE; i++ )
  22.         for( j = 0; j < SIZE; j++ )
  23.             adjacency[i][j] = -1;          
  24.  
  25.     /*  Read input and build adjacency matrix  */
  26.  
  27.     while( scanf("%c",&c) != EOF )
  28.         {
  29.                 if( c == 'c' )
  30.                 {
  31.                         scanf("%d",&num);
  32.                 }
  33.                 else if( c == 'a' )
  34.                 {
  35.                         scanf("%d%d%d",&i,&j,&weight);
  36.             adjacency[i][j] = weight;          
  37.             adjacency[j][i] = weight;          
  38.                 }
  39.         }
  40.    
  41.     NodeList = (int *) malloc(num * sizeof(int));
  42.     BestList = (int *) malloc(num * sizeof(int));
  43.     for( i = 0; i < num; i++ )
  44.     {
  45.         NodeList[i] = i + 1;
  46.         BestList[i] = i + 1;
  47.     }
  48.     best_cost = 1000000;
  49.     permute_list(NodeList,num,num);
  50.         //printf("After checking %d permutations,\n",permutations);
  51.     print_tour(BestList,num,best_cost);
  52. }
  53.  
  54. void permute_list(A,num,n)
  55.    
  56.     int A[],num,n;
  57. {
  58.     int i,j,temp;
  59.     int cost;
  60.     int start = n - num + 1; // if you want to "set" the first city.
  61. //  int start = n - num;    // if you want to try ALL permutations
  62.  
  63.        
  64.  
  65.     if( num == 1 )
  66.     {
  67.         cost = compute_cost(A,n);
  68.         if( cost < best_cost )
  69.         {
  70.             int k;
  71.             best_cost = cost;
  72.             for(k = 0; k < n; k++)
  73.                 BestList[k] = A[k];        
  74.         }
  75.         return;
  76.     }
  77.  
  78.     for(i = start; i < n; i++)
  79.     {
  80.         permute_list(A,num-1,n);
  81.  
  82.         /* rotate the list  */
  83.         temp = A[n-1];
  84.         for( j = n-1; j > start; j-- )
  85.         {
  86.             A[j] = A[j-1];
  87.         }
  88.         A[start] = temp;
  89.  
  90.     }
  91. }
  92.  
  93. int compute_cost(List,num)
  94.     int List[],num;
  95. {
  96.     int cost = 0;
  97.     int i;
  98.  
  99.  
  100.     for(i = 0; i < num-1; i++)
  101.     {
  102.         cost += adjacency[List[i]][List[i+1]];
  103.     }
  104.     cost +=  adjacency[List[i]][List[0]];
  105.  
  106.  //permutations++;
  107.   //print_tour(List,num,cost);
  108.     return cost;
  109. }
  110.  
  111. void print_tour(List,num,cost)
  112.     int List[],num,cost;
  113. {
  114.     int i;
  115.     printf("The tour ");
  116.     for( i = 0; i < num; i++ )
  117.         printf("%d ", List[i]);
  118.  
  119.     printf("%d ", List[0]);
  120.     printf(" costs %d\n",cost);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement