testbtsalesperson.cpp

来自「《数据结构、算法与应用》从C++语言应用角度列举了要点」· C++ 代码 · 共 51 行

CPP
51
字号
// test adjacencyWDigraph::btSalesperson, backtracking algorithm

#include <iostream>
#include <iterator>
#include "adjacencyWDigraph.h"
#include "adjacencyWGraph.h"
#include "weightedEdge.h"

using namespace std;

void main(void)
{
   const int numberOfTypes = 2;
   // create a graph of each adjacency weighted type
   adjacencyWDigraph<int> *g[numberOfTypes];

   // input a test graph
   cout << "Enter number of vertices and edges"<< endl;
   int n, e;
   cin >> n >> e;
   g[0] = new adjacencyWGraph<int>(n, 100);
   g[1] = new adjacencyWDigraph<int>(n, 100);
   for (int i = 1; i <= e; i++)
   {
      cout << "Enter weighted edge " << i << endl;
      int u, v, w;
      cin >> u >> v >> w;
      g[0]->insertEdge(new weightedEdge<int>(u, v, w));
      g[1]->insertEdge(new weightedEdge<int>(u, v, w));
   }

   cout << "The weighted undirected graph is" << endl;
   g[0]->output(cout);
   cout << "\nThe weighted digraph is" << endl;
   g[1]->output(cout);

   // test btSalesperson
   int *p = new int [n + 1];
   cout << "\nThe length of the shortest tour is "
        << g[0]->btSalesperson(p) << endl;
   cout << "The shortest tour is ";
   copy(p + 1, p + n + 1, ostream_iterator<int>(cout, "  "));
   cout << endl;

   cout << "\nThe length of the shortest tour is "
        << g[1]->btSalesperson(p) << endl;
   cout << "The shortest tour is ";
   copy(p + 1, p + n + 1, ostream_iterator<int>(cout, "  "));
   cout << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?