shortpth.cpp

来自「图论:最短路径算法实现 Graph.gph GraphBFS.h Grap」· C++ 代码 · 共 84 行

CPP
84
字号
#include <assert.h>
#include <iostream.h>
#include <stdlib.h>

#include "graphopr.h"
#include "graphbfs.h"


void main()
{
  Graph* G;
  FILE *fp;
  char *filename;

  filename = new char[255];

  cout << "Filename:" << endl;
  cin >> filename;

  if ((fp = fopen(filename, "rt")) == NULL)
  {
    cout << "Unable to open file |" << filename << "|" << endl;
	cout << "To know file format, View sample.gph" << endl;
    exit(-1);
  }

  G = readGraph(fp);

  if (G == NULL)
  {
    cout << "Unable to create graph" << endl;
	cout << "To know file format, View sample.gph" << endl;
    exit(-1);
  }
  
  printGraph(G);

  Queue<int>* Q = new Queue<int>(G->n());

  int* path = new int[G->n()];
  
  int c;

  cout << "1. Continue test ShortestPath" << endl;
  cout << "0. Exit" << endl;
  cout << "----------------------------------------" << endl;
  cout << "Choice:" << endl;

  cin >> c;

  int s, d;

  while (c != 0)
  {
  for (int i = 0; i < G->n(); i++)
	path[i] = UNREACHABLE;

  cout << "Input source node(0 - " << G->n() - 1 << "):" << endl;

  cin >> s;

  while (s < 0 || s >= G->n())
	cin >> s;
  
  cout << "Input destination node(0 - " << G->n() - 1 << "):" << endl;

  cin >> d;

  while (d < 0 || d >= G->n())
	cin >> d;

  shortestPath(G, s, d, Q, path);
  cout << endl;

  cout << "1. Continue test ShortestPath" << endl;
  cout << "0. Exit" << endl;
  cout << "----------------------------------------" << endl;
  cout << "Choice:" << endl;

  cin >> c;

  }
 
}

⌨️ 快捷键说明

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