double calc_dist(std::vector<tLineSegment<point>> &L,size_t i, size_t j) { /* static std::unordered_map<size_t,double> cache; //@REMARK: magic statics are not thread-safe at Microsoft std::unordered_map<size_t,double>::const_iterator it; it = cache.find(key); if (it != cache.end()) { return (*it).second; }*/ //@TODO: refactor #define li1 L[i].s #define li2 L[i].e #define lj1 L[j].s #define lj2 L[j].e double len_i = d_euc(li1,li2); double len_j = d_euc(lj1,lj2); // total_dist: larger segement first. double td = (len_i >= len_j)?total_dist(li1,li2,lj1,lj2):total_dist(lj1,lj2,li1,li2); /*if (cache.size() < 256E3/4) cache.insert(std::pair<size_t, double>(key, td));*/ return td; }
void dfs(int step, int city) { // city is the current city for step, i.e. the starting point of this step //II("step = %d, city = %d", step, city); dfs_cnt++; int i; ans[0][step] = city; use[city] = 1; int low_bound = bound(step); if (low_bound >= best) { // bound use[city] = 0; return; } if (step == n - 1) { int total = total_dist(ans[0]); //fprintf(stderr, "total = %d\n", total); if (!best || best > total) { best = total; memcpy(ans[1], ans[0], n * sizeof(int)); } } else { for (i = 1; i < n; i++) { // 0 is the city of itself if (low_bound < 0 && best > 0) { low_bound = bound(step); } if (low_bound >= best) { break; } if (!use[d[2][city][i]]) { dfs(step + 1, d[2][city][i]); // branch } } } use[city] = 0; }
void output() { printf("%d\n", total_dist(ans[1])); for (i = 0; i < n; i++) { printf("%d ", ans[1][i] + 1); } printf("1 \n"); }
void perm_sub(int n,int lst[]){ int i; if(n==LENGTH){ double dist = total_dist(lst); if(dist<sum){ sum = dist; copy_solution(lst); } }else{ for(i=1;i<LENGTH;i++){ if(!(apper(lst,i,num))){ if(n!=2 || lst[0]>i){ append(lst,i,num); num++; perm_sub(n+1,lst); num--; } } } } }