并行

来源:互联网 发布:centos 安装ant 编辑:程序博客网 时间:2024/05/02 00:42

// Tsp.cpp : 定义控制台应用程序的入口点。

// Tsp.cpp : 定义控制台应用程序的入口点。////============================================================================// Name        : Tsp.cpp// Author      : mtt// Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include<windows.h>#include <iostream>#include <fstream>#include <stdlib.h>#include <time.h>#include <cstring>//#include <math.h>//#define   CLOCKS_PER_SEC ((clock_t)1000)#define MPICH_SKIP_MPICXX  12#include"mpi.h"using namespace std;#define Max 6000#define a_len 10const int MaxSize=45;template <class T>class MGraph{   public:MGraph( );     ~MGraph(){};     void DFSTraverse(int v);     void BFSTraverse(int v); void Grefenstette (int a); void Distance (double *W);   private:       T vertex[MaxSize+1];       int arc[MaxSize][MaxSize];   char cities[MaxSize][MaxSize];       //int vertexNum, arcNum;       static int visited[MaxSize],Q[MaxSize];};template <class T>int MGraph<T>::visited[MaxSize]={0};template <class T>int MGraph<T>::Q[MaxSize]={0};template <class T>//链接矩阵的构造更好函数MGraph<T>::MGraph(){int i;   for ( i=0; i<MaxSize; i++)       vertex[i]=i+1;    ifstream fin("ChinaCites1", ios_base::binary);              for (  i=0;i<MaxSize;i++)     {     for (int j=0;j<MaxSize ;j++)     {                 fin>> arc[i][j];      //初始化邻接矩阵//边依附的两个顶点的序号之间的距离,     }     }         fin.close();ifstream fin2("Chaina45",ios::binary|ios::in);for (  i=0;i<MaxSize;i++){           fin2>>cities[i]; } fin2.close();}template <class T>void MGraph<T>::DFSTraverse(int v)//深度优先{     cout<<vertex[v]<<endl;      visited [v]=1;      for (int j=0; j<MaxSize; j++)          if (arc[v][j]!=0 && visited[j]==0)            DFSTraverse( j );}template <class T>void MGraph<T>::BFSTraverse(int v){    int front =-1;    int rear =-1;   //假设采用顺序队列且不会发生溢出    cout<<vertex[v]<<endl;    visited[v]=1;    Q[++rear]=v;    while (front!=rear)     {         v=Q[++front];         for (int j=0; j<MaxSize; j++)            if (arc[v][j]!=0 && visited[j]==0 )            {                  cout<<vertex[j]<<endl;  visited[j]=1;                  Q[++rear]=j;            }      }}template <class T>//对染色体的Grefenstette编码规则void MGraph<T>::Grefenstette(int a){ static int t=0;static int seed=a;    srand((unsigned)time(0)+(seed++));         int tmp,i,w=0;  for( i=1;i<=MaxSize;i++)vertex[i]=i; for(int j=0;j<MaxSize-1;j++){tmp=rand()%(MaxSize-j)+j+1;swap<int>(vertex[j],vertex[tmp]); }       //  for( i=0;i<MaxSize;i++)//cout<<vertex[i]<<"\t"<<w++<<endl; //写到Excel中保存            FILE * xlsfile;            xlsfile = fopen("数组输出.xls","ab");fprintf(xlsfile,"路线 %d\t",++t);for( i=0;i<MaxSize;i++) fprintf(xlsfile,"%s\t",cities[vertex[i]-1]);fprintf(xlsfile,"\n");fclose(xlsfile);}template <class T>void MGraph<T>::Distance (double  W[]){  static int t=1;int i,j=0;   double n=0.0;for ( i=1;i<MaxSize;i++){ int a=vertex[i-1]-1,b=vertex[i]-1;n+=arc[a][b];}   W[t++]=n;//cout<<"路径之和:"<<n<<endl; FILE * xlsfile;  xlsfile = fopen("数组输出.xls","ab");   fprintf(xlsfile,"路径之和:%lf\t",n);    fprintf(xlsfile,"\n");   fclose(xlsfile);  }void show (double  W[]){for(int i=1;i<=Max;i++)cout<<W[i]<<endl;cout<<endl;}//快速排序long  Partition (double  r[],long  first,long  end){double  t;    while (first<end){while (first<end&&r[first]<=r[end])end--;if(first<end){    t=r[first];r[first]=r[end];r[end]=t;               first++;}while (first<end&&r[first]<=r[end])first++;if(first<end){    t=r[first];r[first]=r[end];r[end]=t;                   end--;}}return first;}void QuickSort (double   r[ ], long  first, long  end ){long  pivotpos;   if(first<end){    pivotpos = Partition (r, first, end );       QuickSort (r,first,pivotpos-1);          QuickSort (r, pivotpos+1, end );          }}//------------------------------------------------------------------------------int main(int argc, char* argv[]) {//int **p=(int **)malloc(sizeof (int *)*max);//   for (int i=0;i<max;i++)//{// p[i]=(int *)malloc(sizeof(int)*max);//               }  int myid,numprocs;  MPI_Request  request ;  int dest,source;  int bestpath = 1;  MPI_Init(&argc,&argv);  MPI_Comm_size(MPI_COMM_WORLD,&numprocs);  MPI_Comm_rank(MPI_COMM_WORLD,&myid);   cout<<"Process "<<myid<<" begins!"<<endl;   MGraph<int>mgraph;   clock_t start, end;  double W[Max+1]={0.0};  // mgraph.DFSTraverse(0);  //mgraph.BFSTraverse(40);   start = clock();for (int i=0;i<Max;i++){mgraph.Grefenstette(myid+1000);   mgraph.Distance(W);}//show(W); QuickSort (W, 1, Max );//show(W);cout<<"最短路径: "<<W[1]<<endl;  end=clock();  MPI_Finalize();   cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"  秒"<<endl;int a;cin>>a;return 0;}


原创粉丝点击