void deletePaths(CDGPath * path) { assert(NULL != path); CDGPath *next; do { next = getNextPath(path); deleteNodeList(getPathNode(path)); setNextPath(path, NULL); free(path); path = next; } while (path); }
int main(int argc, char **argv) { mint maxLen = 0; mint nNodes = 0; mint ** coords; mdouble ** dists; string file = "../examples/pub02"; string s; char buff[6]; stringstream ss; #if DBG string inadr = file; ifstream inFile (inadr.append(".in").data()); getline(inFile,s); #else getline(cin,s); #endif ss << s; ss.getline(buff,6,' '); nNodes = (mint)(atoi(buff)); // TODO buff se mozna musi po pouziti zas smazat ss.getline(buff,6,' '); maxLen = (mint)(atoi(buff)); //scout << nNodes << " " << maxLen << endl; coords = new mint * [nNodes]; for(mint i=0;i<nNodes;i++){ coords[i] = new mint[2]; } dists = new mdouble * [nNodes]; for(mint i=0;i<nNodes;i++){ dists[i] = new mdouble[nNodes]; } #if DBG for(mint i=0;i<nNodes;i++){ s.clear(); ss.clear(); getline(inFile,s); ss << s; ss.getline(buff,6,' '); coords[i][0] = (mint)(atoi(buff)); ss.getline(buff,6,' '); coords[i][1] = (mint)(atoi(buff)); } inFile.close(); #else for(mint i=0;i<nNodes;i++){ s.clear(); ss.clear(); getline(cin,s); ss << s; ss.getline(buff,6,' '); coords[i][0] = (mint)(atoi(buff)); ss.getline(buff,6,' '); coords[i][1] = (mint)(atoi(buff)); } #endif #if DBG ifstream fileStream (file.append(".out").data()); s.clear(); getline(fileStream,s); cout << "Correct result: " << s << endl; fileStream.close(); #endif /*/ for(mint i=0;i<nNodes;i++){ for(mint j=0;j<2;j++){ cout << coords[i][j] << " "; } cout << endl; } /**/ for(mint i=0;i<nNodes;i++){ for(mint j=0;j<nNodes;j++){ if(j>i){ dists[i][j]=dist(coords[i][0],coords[i][1],coords[j][0],coords[j][1]); } else if (j<i) { dists[i][j] = dists[j][i]; } else { dists[i][i]=0; } } } mint **** conflicts = new mint *** [nNodes]; for(mint i=0;i<nNodes;i++){ conflicts[i] = new mint ** [nNodes]; for(mint j=0;j<nNodes;j++){ conflicts[i][j] = new mint * [nNodes]; for(mint k=0;k<nNodes;k++){ conflicts[i][j][k] = new mint[nNodes]; for(mint l=0;l<nNodes;l++){ conflicts[i][j][k][l]=intersects(coords[i],coords[j],coords[k],coords[l]); } } } } /*for(mint i=0;i<nNodes;i++){ for(mint j=0;j<nNodes;j++){ cout << dists[i][j] << " "; } cout << endl; }/**/ list<Node *> * oldGen = new list<Node *>(); list<Node *> * newGen = new list<Node *>(); list<Node *>::iterator it; mint level = 0; // bit mask of available vertices mint avVertices = pow(2,nNodes) - 1; for(mint i = 0;i<nNodes;i++){ Node * n = new Node(0); n->avNodesMask = avVertices xor 1 << i; // cout <<"Node #: " << i << " AvNodeMask: " << (bitset<16>) n->avNodesMask << endl; n->nodes->push_back(i); oldGen->push_back(n); } for(it=oldGen->begin();it!=oldGen->end();++it){ // cout << "next from old: " << endl; // (*it)->printNode(); for(mint i=0;i<nNodes;i++){ // cout << "next to add: " << i << endl; if((*it)->avNodesMask & 1 << i){ mdouble len = (*it)->len + dists[(*it)->nodes->back()][i]; Node * n = new Node(len); list<mint>::iterator ns; ns = (*it)->nodes->begin(); for(ns;ns!=(*it)->nodes->end();++ns){ n->nodes->push_back(*ns); //cout << *ns << endl; } n->nodes->push_back(i); n->avNodesMask = (*it)->avNodesMask xor 1 << i; // cout << "added: " << i << endl; newGen->push_back(n); //break; } } newGen->pop_back(); } deleteNodeList(oldGen); oldGen->clear(); delete oldGen; oldGen=newGen; level++; /**/ while(level<nNodes){ // oldGenSize = oldGen->size(); newGen = new list<Node *>(); for(it=oldGen->begin();it!=oldGen->end();++it){ // cout << "next from old: " << endl; // (*it)->printNode(); for(mint i=0;i<nNodes;i++){ // cout << "next to add: " << i << endl; if((*it)->avNodesMask & 1 << i){ if(possible(conflicts,(*it)->nodes,i)){ mdouble addedToFirstDist = dists[i][(*it)->nodes->front()]; mdouble len = (*it)->len + dists[(*it)->nodes->back()][i]; if (len + addedToFirstDist <= maxLen) { Node * n = new Node(len); list<mint>::iterator ns; ns = (*it)->nodes->begin(); for(ns;ns!=(*it)->nodes->end();++ns){ n->nodes->push_back(*ns); //cout << *ns << endl; } n->nodes->push_back(i); n->avNodesMask = (*it)->avNodesMask xor 1 << i; // cout << "added: " << i << endl; newGen->push_back(n); //break; } } } } } if(newGen->empty() || level+1 == nNodes){ it = oldGen->begin(); list<mint>::iterator iter; Node * chosen = *it; mdouble mLen = (*it)->len + dists[chosen->nodes->front()][chosen->nodes->back()]; mint n = (*it)->nodes->size(); for(it;it!=oldGen->end();++it){ mdouble tmp = (*it)->len + dists[(*it)->nodes->front()][(*it)->nodes->back()]; if (tmp < mLen){ mLen = tmp; chosen = *it; } } cout << (int)ceil(mLen) << endl; break; } deleteNodeList(oldGen); oldGen->clear(); delete oldGen; oldGen=newGen; level++; }/**/ for(mint i=0;i<nNodes;i++) delete [] coords[i]; for(mint i=0;i<nNodes;i++) delete [] dists[i]; for(mint i=0;i<nNodes;i++){ for(mint j=0;j<nNodes;j++){ for(mint k=0;k<nNodes;k++){ delete [] conflicts[i][j][k]; } delete [] conflicts[i][j]; } delete [] conflicts[i]; } delete [] dists; delete [] coords; delete [] conflicts; deleteNodeList(oldGen); delete oldGen; // deleteNodeList(newGen); // delete newGen; return 0; }