pastebin - collaborative debugging tool
kpaste.net RSS


Untitled
Posted by Anonymous on Sun 4th Dec 2011 03:18
raw | new post
view followups (newest first): Untitleds by Anonymous

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define TRUE  1
  6. #define FALSE 0
  7. #define DEBUG 0
  8.  
  9. #define MAX_TOWNS 752
  10.  
  11. typedef struct Highway Highway;
  12. struct Highway {
  13.   int    from, to, done, old;
  14.   double distance;
  15. };
  16.  
  17. typedef struct City City;
  18. struct City {
  19.   int x, y, connected;
  20. };
  21.  
  22. int     highways_done;
  23. int     highways_count;
  24. Highway highways[(MAX_TOWNS*MAX_TOWNS)/2];
  25.  
  26. int sets_count;
  27. int sets[MAX_TOWNS];
  28.  
  29. int  cities_count;
  30. int  cities_connected;
  31. City cities[MAX_TOWNS];
  32.  
  33. double distance(int from, int to){
  34.   return sqrt((cities[from].x-cities[to].x)*((cities[from].x-cities[to].x))+(cities[from].y-cities[to].y)*(cities[from].y-cities[to].y));
  35. }
  36.  
  37. Highway *highway(int from, int to){
  38.   if(from == to) return NULL;
  39.  
  40.   if(from < to){
  41.     return &highways[from+(to-1)*to/2];
  42.   }else{
  43.     return highway(to, from);
  44.   }
  45. }
  46.  
  47. int create_cicles(Highway *hw){
  48.   if(sets[hw->from] == -1)           return FALSE;
  49.   if(sets[hw->to]   == -1)           return FALSE;
  50.   if(sets[hw->from] == sets[hw->to]) return TRUE;
  51.   return FALSE;
  52. }
  53.  
  54. void merge_sets(int from, int to){
  55.   int i, set_id;
  56.  
  57.   if(sets[from] == -1 && sets[from] == sets[to]){
  58.     sets[to]   = sets_count;
  59.     sets[from] = sets_count;
  60.     sets_count++;
  61.     return;
  62.   }
  63.  
  64.   if(sets[from] == -1){
  65.     sets[from] = sets[to];
  66.     return;
  67.   }
  68.  
  69.   if(sets[to] == -1){
  70.     sets[to] = sets[from];
  71.     return;
  72.   }
  73.  
  74.   if(sets[from] == sets[to]) return;
  75.  
  76.   for(i = 0; i < cities_count; i++){
  77.     set_id = sets[to];
  78.     if(set_id == sets[i]){
  79.       sets[i] = sets[from];
  80.     }
  81.   }
  82.   if(sets_count > 1) sets_count--;
  83. }
  84.  
  85. int ended(){
  86.   if(cities_connected == cities_count && sets_count == 1) return TRUE;
  87.   return FALSE;
  88. }
  89.  
  90. void build_highway(Highway *hw){
  91.   if(!cities[hw->to].connected){
  92.     cities[hw->to].connected = TRUE;
  93.     cities_connected++;
  94.   }
  95.  
  96.   if(!cities[hw->from].connected){
  97.     cities[hw->from].connected = TRUE;
  98.     cities_connected++;
  99.   }
  100.  
  101.   merge_sets(hw->from, hw->to);
  102.   hw->done = TRUE;
  103.   highways_done++;
  104. }
  105.  
  106. void add_highway_by_cities(int from, int to){
  107.   highway(from, to)->old = TRUE;
  108.   build_highway( highway(from, to) );
  109. }
  110.  
  111. void initialize_highways(){
  112.   int from, to;
  113.   Highway *hw;
  114.  
  115.   highways_count = 0;
  116.   for(to = 1; to < cities_count; to++){
  117.     for(from = 0; from < to; from++){
  118.       highways_count++;
  119.       hw = highway(from, to);
  120.  
  121.       hw->to       = to;
  122.       hw->old      = FALSE;
  123.       hw->from     = from;
  124.       hw->done     = FALSE;
  125.       hw->distance = distance(from, to);
  126.     }
  127.   }
  128. }
  129.  
  130. int compare_highways(const void *highway_a, const void *highway_b){
  131.   // if(((Highway*)highway_a)->done) return 1;
  132.   // if(((Highway*)highway_b)->done) return -1;
  133.   if(((Highway*)highway_a)->distance > ((Highway*)highway_b)->distance) return 1;
  134.   if(((Highway*)highway_a)->distance < ((Highway*)highway_b)->distance) return -1;
  135.   return 0;
  136. }
  137.  
  138. void debug_highways(){
  139.   int i;
  140.   printf("\n\n");
  141.   for(i = 0; i < highways_count; i++){
  142.     printf("Highway: (%3d, %3d) %d %d %lf\n", highways[i].from, highways[i].to, highways[i].done, highways[i].old, highways[i].distance);
  143.   }
  144. }
  145.  
  146. void kruskal(){
  147.   int i;
  148.   Highway *hw;
  149.   qsort(highways, highways_count, sizeof(Highway), compare_highways);
  150.   if(DEBUG) debug_highways();
  151.  
  152.   for(i = 0; i < highways_count; i++){
  153.     if( !highways[i].done && !create_cicles(&highways[i]) ){
  154.       if(DEBUG) printf("(%3d,%3d)-(%3d,%3d)-(%d==%d, %d)\n", highways[i].from, highways[i].to, sets[highways[i].from], sets[highways[i].to], cities_connected, cities_count, sets_count);
  155.       build_highway(&highways[i]);
  156.       printf("%d %d\n", highways[i].from+1, highways[i].to+1);
  157.       if(ended()) break;
  158.     }
  159.   }
  160. }
  161.  
  162. int main(){
  163.   int i, j, from, to;
  164.   int test_cases;
  165.  
  166.   scanf("%d", &test_cases);
  167.  
  168.   for(i = 0; i < test_cases; i++){
  169.     scanf("%d", &cities_count);
  170.     sets_count       = 0;
  171.     cities_connected = 0;
  172.     for(j = 0; j < cities_count; j++){
  173.       scanf("%d %d", &(cities[j].x), &(cities[j].y));
  174.       cities[j].connected = FALSE;
  175.       sets[j] = -1;
  176.     }
  177.  
  178.     initialize_highways();
  179.     scanf("%d", &highways_done);
  180.     for(j = highways_done; j > 0; j--){
  181.       scanf("%d %d", &from, &to);
  182.       if(from == to){
  183.         highways_done--;
  184.       }else{
  185.         if(highway(from-1, to-1)->done){
  186.           highways_done--;
  187.         }else{
  188.           add_highway_by_cities(from-1, to-1);
  189.         }
  190.       }
  191.     }
  192.  
  193.     // printf("(%d==%d, %d)\n", cities_connected, cities_count, sets_count);
  194.  
  195.     if(ended() || cities_count == 1){
  196.       printf("No new highways need\n");
  197.     }else{
  198.       if(DEBUG) debug_highways();
  199.       kruskal();
  200.       if(DEBUG) debug_highways();
  201.     }
  202.  
  203.     if(i+1 < test_cases) printf("\n");
  204.   }
  205.  
  206.   return 0;
  207. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at