pastebin - collaborative debugging tool
kpaste.net RSS


Untitleds
Posted by Anonymous on Sun 4th Dec 2011 03:23
raw | new post
modification of post by Anonymous (view diff)

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

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