pastebin - collaborative debugging tool
kpaste.net RSS

Difference between
modified post 30bde by Anonymous on Sun 4th Dec 2011 03:23
original post 9dee88bce by Anonymous on Sun 4th Dec 2011 03:18
Download diff
Show old version | new version | both versions

    
11
#include <math.h>
22
#include <stdio.h>
33
#include <stdlib.h>
44
55
#define TRUE  1
66
#define FALSE 0
77
#define DEBUG 0
88
99
#define MAX_TOWNS 752
1010
1111
typedef struct Highway Highway;
1212
struct Highway {
1313
  int    from, to, done, old;
1414
  double distance;
1515
};
1616
1717
typedef struct City City;
1818
struct City {
1919
  int x, y, connected;
2020
};
2121
2222
int     highways_done;
2323
int     highways_count;
2424
Highway highways[(MAX_TOWNS*MAX_TOWNS)/2];
2525
2626
int sets_count;
27+
int sets_luid;
2728
int sets[MAX_TOWNS];
2829
2930
int  cities_count;
3031
int  cities_connected;
3132
City cities[MAX_TOWNS];
3233
3334
double distance(int from, int to){
3435
  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));
3536
}
3637
3738
Highway *highway(int from, int to){
3839
  if(from == to) return NULL;
3940
4041
  if(from < to){
4142
    return &highways[from+(to-1)*to/2];
4243
  }else{
4344
    return highway(to, from);
4445
  }
4546
}
4647
4748
int create_cicles(Highway *hw){
4849
  if(sets[hw->from] == -1)           return FALSE;
4950
  if(sets[hw->to]   == -1)           return FALSE;
5051
  if(sets[hw->from] == sets[hw->to]) return TRUE;
5152
  return FALSE;
5253
}
5354
5455
void merge_sets(int from, int to){
5556
  int i, set_id;
5657
5758
  if(sets[from] == -1 && sets[from] == sets[to]){
58-
    sets[to]   = sets_count;
59+
    sets[to]   = sets_luid;
59-
    sets[from] = sets_count;
60+
    sets[from] = sets_luid;
61+
    sets_luid++;
6062
    sets_count++;
6163
    return;
6264
  }
6365
6466
  if(sets[from] == -1){
6567
    sets[from] = sets[to];
6668
    return;
6769
  }
6870
6971
  if(sets[to] == -1){
7072
    sets[to] = sets[from];
7173
    return;
7274
  }
7375
7476
  if(sets[from] == sets[to]) return;
7577
76-
  for(i = 0; i < cities_count; i++){
7778
    set_id = sets[to];
79+
  for(i = 0; i < cities_count; i++){
7880
    if(set_id == sets[i]){
7981
      sets[i] = sets[from];
8082
    }
8183
  }
8284
  if(sets_count > 1) sets_count--;
8385
}
8486
8587
int ended(){
8688
  if(cities_connected == cities_count && sets_count == 1) return TRUE;
8789
  return FALSE;
8890
}
8991
9092
void build_highway(Highway *hw){
9193
  if(!cities[hw->to].connected){
9294
    cities[hw->to].connected = TRUE;
9395
    cities_connected++;
9496
  }
9597
9698
  if(!cities[hw->from].connected){
9799
    cities[hw->from].connected = TRUE;
98100
    cities_connected++;
99101
  }
100102
101103
  merge_sets(hw->from, hw->to);
102104
  hw->done = TRUE;
103105
  highways_done++;
104106
}
105107
106108
void add_highway_by_cities(int from, int to){
107109
  highway(from, to)->old = TRUE;
108110
  build_highway( highway(from, to) );
109111
}
110112
111113
void initialize_highways(){
112114
  int from, to;
113115
  Highway *hw;
114116
115117
  highways_count = 0;
116118
  for(to = 1; to < cities_count; to++){
117119
    for(from = 0; from < to; from++){
118120
      highways_count++;
119121
      hw = highway(from, to);
120122
121123
      hw->to       = to;
122124
      hw->old      = FALSE;
123125
      hw->from     = from;
124126
      hw->done     = FALSE;
125127
      hw->distance = distance(from, to);
126128
    }
127129
  }
128130
}
129131
130132
int compare_highways(const void *highway_a, const void *highway_b){
131133
  // if(((Highway*)highway_a)->done) return 1;
132134
  // if(((Highway*)highway_b)->done) return -1;
133135
  if(((Highway*)highway_a)->distance > ((Highway*)highway_b)->distance) return 1;
134136
  if(((Highway*)highway_a)->distance < ((Highway*)highway_b)->distance) return -1;
135137
  return 0;
136138
}
137139
138140
void debug_highways(){
139141
  int i;
140142
  printf("\n\n");
141143
  for(i = 0; i < highways_count; i++){
142144
    printf("Highway: (%3d, %3d) %d %d %lf\n", highways[i].from, highways[i].to, highways[i].done, highways[i].old, highways[i].distance);
143145
  }
144146
}
145147
146148
void kruskal(){
147149
  int i;
148150
  Highway *hw;
149151
  qsort(highways, highways_count, sizeof(Highway), compare_highways);
150152
  if(DEBUG) debug_highways();
151153
152154
  for(i = 0; i < highways_count; i++){
153155
    if( !highways[i].done && !create_cicles(&highways[i]) ){
154156
      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);
155157
      build_highway(&highways[i]);
156158
      printf("%d %d\n", highways[i].from+1, highways[i].to+1);
157159
      if(ended()) break;
158160
    }
159161
  }
160162
}
161163
162164
int main(){
163165
  int i, j, from, to;
164166
  int test_cases;
165167
166168
  scanf("%d", &test_cases);
167169
168170
  for(i = 0; i < test_cases; i++){
169171
    scanf("%d", &cities_count);
170172
    sets_count       = 0;
173+
    sets_luid        = 0;
171174
    cities_connected = 0;
172175
    for(j = 0; j < cities_count; j++){
173176
      scanf("%d %d", &(cities[j].x), &(cities[j].y));
174177
      cities[j].connected = FALSE;
175178
      sets[j] = -1;
176179
    }
177180
178181
    initialize_highways();
179182
    scanf("%d", &highways_done);
180183
    for(j = highways_done; j > 0; j--){
181184
      scanf("%d %d", &from, &to);
182185
      if(from == to){
183186
        highways_done--;
184187
      }else{
185188
        if(highway(from-1, to-1)->done){
186189
          highways_done--;
187190
        }else{
188191
          add_highway_by_cities(from-1, to-1);
189192
        }
190193
      }
191194
    }
192195
193196
    // printf("(%d==%d, %d)\n", cities_connected, cities_count, sets_count);
194197
195198
    if(ended() || cities_count == 1){
196199
      printf("No new highways need\n");
197200
    }else{
198201
      if(DEBUG) debug_highways();
199202
      kruskal();
200203
      if(DEBUG) debug_highways();
201204
    }
202205
203206
    if(i+1 < test_cases) printf("\n");
204207
  }
205208
206209
  return 0;
207210
}
208211

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