從TSPLib中提取出鄰接矩陣

來源:互聯網
上載者:User

#include<cmath>#include<iostream>#include<fstream>#include<string>#include<sstream>using namespace std;class City{private:string filename;int dimension;string comment;void cityGenerate(char* s);public:City(char* s){cityGenerate(s);}double **city;enum dt{COORD_DISPLAY,NODISPLAY,TWOD_DISPLAY}displayType;enum ewt{EUC_2D,GEO,EXPLICIT,CEIL_2D,others};enum ewf{LOWER_DIAG_ROW,UPPER_ROW,FULL_MATRIX,FUNCTION};enum ewt edgeWeightType;enum ewf edgeWeightFormat;//~City(){}string getFilename(){return filename;}void setFilename(string name){filename=name;}int getDimension(){return dimension;}void setDimension(int d){dimension=d;}enum ewt getewt(){return edgeWeightType;}void setewt(enum ewt ewtype){edgeWeightType=ewtype;}enum ewf getewf(){return edgeWeightFormat;}void setewf(ewf ewformat){edgeWeightFormat=ewformat;}string getComment(){return comment;}void setComment(string com){comment=com;}void setDisplaytype(dt distype){displayType=distype;}enum dt getDisplaytype(){return displayType;}template <class T>  //T為點座標的資料結構,可以為int或doubleinline double distance(T x1,T y1,T x2,T y2){T tempx=x1-x2,tempy=y1-y2;//cout<<endl<<"tempx"<<tempx<<endl;cout<<endl<<"tempy"<<tempy<<endl;return sqrt(tempx*tempx+tempy*tempy);}void showCityMatrix();void showUpperCityMatrix();void showLowerCityMatrix();//bool meetCriterion;};void City::showCityMatrix(){cout<<"city matrix:"<<endl;for(int i=0;i<dimension;i++){for(int j=0;j<dimension;j++)cout<<city[i][j]<<'\t';cout<<endl;}}void City::showUpperCityMatrix(){cout<<"city upper matrix:"<<endl;for(int i=0;i<dimension;i++){for(int j=i+1;j<dimension;j++)cout<<city[i][j]<<'\t';cout<<endl;}}void City::showLowerCityMatrix(){cout<<"city Lower matrix:"<<endl;for(int i=0;i<dimension;i++){for(int j=0;j<=i;j++)cout<<city[i][j]<<'\t';}}//void City::cityGenerate(char* s,City city);void main(){City c("bays29.tsp");return;}void City:: cityGenerate(char* s){cout<<"TSP路徑檔案名稱:"<<s<<endl;cout<<"--------------------------------------------"<<endl;ifstream in(s);for(string line;getline(in,line);){string content;int d=0;for(istringstream sin(line);sin>>content;){if(content=="NAME:"){sin>>content;setFilename(content);}if(content=="COMMENT:"){string comment;while(sin>>content){comment.append (content);comment.append(" ");}setComment(comment);//cout<<endl<<"評論:"<<city.getComment()<<endl;}if(content=="DIMENSION:"){sin>>d;setDimension(d);//城市矩陣初始化city=new double*[d];for(int i=0;i<d;i++){city[i]=new double[d];}}if(content=="EDGE_WEIGHT_TYPE:"){sin>>content;if(content=="EUC_2D")setewt(EUC_2D);elseif(content=="GEO")setewt(GEO);elseif(content=="EXPLICIT")setewt(EXPLICIT);elseif(content=="CEIL_2D")setewt( CEIL_2D);elsesetewt(others);}if(content=="EDGE_WEIGHT_FORMAT:"){sin>>content;if(content=="LOWER_DIAG_ROW")setewf(LOWER_DIAG_ROW);elseif(content=="UPPER_ROW")setewf(UPPER_ROW);elseif(content=="FUNCTION")setewf(FUNCTION);elseif(content=="FULL_MATRIX")setewf(City:: FULL_MATRIX);}if(content=="DISPLAY_DATA_TYPE:"){sin>>content;if(content=="COORD_DISPLAY")setDisplaytype(COORD_DISPLAY);elseif(content=="TWOD_DISPLAY")setDisplaytype(TWOD_DISPLAY);elsesetDisplaytype(NODISPLAY);}//城市矩陣建立1d=getDimension();if(content=="NODE_COORD_SECTION"){double* x=new double[d],*y=new double[d];for(int i=0;i<d;i++){getline(in,line);istringstream inn(line);inn>>x[i]>>x[i];inn>>y[i];}for(i=0;i<d;i++){for(int j=i;j<d;j++){city[i][j]=distance(x[i],y[i],x[j],y[j]);city[j][i]=city[i][j];}}/**/}//城市矩陣建立1else if(content=="EDGE_WEIGHT_SECTION"){if(getewf()==UPPER_ROW){for(int i=0;i<d;i++){getline(in,line);istringstream inn(line);city[i][i]=0;for(int j=i+1;j<d;j++){double b;inn>>b;city[i][j]=b;city[j][i]=city[i][j];}}//for}if(getewf()==LOWER_DIAG_ROW){int i=0,j=0;while(getline(in,line)){for(istringstream inn(line);inn>>city[i][j];){city[j][i]=city[i][j];j++;if(j>i){i++;j=0;}}}}if(getewf()==FULL_MATRIX){for(int i=0;i<d;i++){getline(in,line);istringstream inn(line);for(int j=0;j<d;j++){double b;inn>>b;city[i][j]=b;}}//for}}}//istringstream串流分析}//每行內容分析in.close();return;}/**/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.