0

Hi all, I asked about this question few days ago, and I have tried few things other experts told me

I think i got overcame problem where i can read the tab spaced file but now the output is the problem.....

When i run my program it outputs:

Enter filename: qantum.txt
Enter start city: Sydney

(so far its okay but...)

Cheapest price to New York = 1300

it outputs only one result where it should outputs all the city which is connected with Sydney....

The following output is what it should be:

Enter filename : qantum.txt
Enter start city : Tokyo
Cheapest price to Brussels = 700
Tokyo Paris Brussels
Cheapest price to London = 900
Tokyo Paris London
Cheapest price to Melbourne = 2200
Tokyo Paris London Sydney Melbourne
Cheapest price to New York = 500
Tokyo New York
Cheapest price to Paris = 600
Tokyo Paris
Cheapest price to Sydney = 2000
Tokyo Paris London Sydney


I'll upload my code so plz help me..

Attachments
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <vector>

using namespace std;

class Student
{
public:
   string name;
   double tuition;
};

//This function sorts by tuitions fees
bool SortStudentByTute ( const Student& left, const Student& right )
{
   if ( left.tuition > right.tuition )
   {
      return false;
   }
   else if ( left.tuition < right.tuition )
   {
      return true;
   }
} 

bool SortStudentByName ( const Student& left, const Student& right )
{
   //Here you can define whatever the sort criterion
   //needs to be. It can be as complicated as you wish
 
   if ( left.name > right.name )
   {
      return false;
   }
   else if ( left.name < right.name )
   {
      return true;
   }
   //else //i.e if both are equal
 
}



class Edge;

class Node
{
public:
   string    name;
   list<Edge*> neighbors;
   bool      visited;
   double       cost;
   Node      *back_pointer;

   Node ( string const &n ) : name ( n ), visited ( false ), cost ( 0 )
   {}
};

class Edge
{
public:
   double weight;
   Node * dest;

   Edge ( double c, Node *d = NULL ) : weight ( c ), dest ( d )
   {}

   ~Edge()
   {
      if ( dest ) delete dest;
   }
};

class NodeMap
{
public:
   map<string, Node*> nodemap;

   Node* find_in_nodemap ( string const &name )
   {
      Node* result = nodemap[name];
      if ( result == 0 )
      {
         result = nodemap[name] = new Node ( name );
      }
      return result;
   }
   friend ostream& operator<< ( ostream& o, NodeMap nm )
   {
      map<string, Node*>::iterator im;
      for ( im = nm.nodemap.begin(); im != nm.nodemap.end(); im++ )
      {
         //pair<string,Node*> p = *im;
         //o << p.second->name << endl;
         o << ( *im ).second->name << endl;
         list<Edge*> neighbors = ( *im ).second->neighbors;
         list<Edge*>::iterator e;
      }
      return o;
   }
};

struct compare
{

   bool operator() ( Node * &a, Node * &b ) const
   {
      // least to greatest
      return b->cost < a->cost;
   }
};
// for each solution, reset node information
void reset_nodes ( NodeMap &nm )
{
   map<string, Node*>::iterator im;
   for ( im = nm.nodemap.begin(); im != nm.nodemap.end(); im++ )
   {
      ( *im ).second->cost = 0;
      ( *im ).second->back_pointer = 0;
      ( *im ).second->visited = false;
   }
}

void dijkstra ( string s, NodeMap &nodes )
{
   Student myStudent [500]; //create an array of objects

   int i = 0;

   // check and report or abort
   Node* source = nodes.nodemap[s];
   if ( source == 0 )
   {
      cout << "Sorry we don't fly from " << s << endl;
      return;
   }
   reset_nodes ( nodes );

   // put the source into pq and loop until empty
   priority_queue<Node *, deque<Node*>, compare > pq;
   pq.push ( source );
   while ( !pq.empty() )
   {
      // process least cost node.
      Node* curr = pq.top();
      pq.pop();
      curr->visited = true;
      // process neighbors
      list<Edge*>::iterator edge; 
      for ( edge = curr->neighbors.begin(); edge != curr->neighbors.end(); edge++ )
      {
         Node *ne = ( *edge )->dest;
         if ( !ne->visited )
         {
            ne->cost += ( *edge )->weight + curr->cost;
            ne->visited = true;
            ne->back_pointer = curr;
            //cout << "Cheapest price to "<< ne->name << " = " << ne->cost << endl; 

            myStudent[i].name = ne->name;
            myStudent[i].tuition = ne->cost;
            i++; 


            pq.push ( ne );
         }
      }
   }

   std::sort ( myStudent, myStudent + i, SortStudentByName );
   for ( int j = 0; j < i; j++ )
   {
      cout << "Cheapest price to " << myStudent[j].name << " = " << myStudent[j].tuition << endl;
   }
}

void get_graph ( string const &filename, NodeMap &node_map )
{
   ifstream inf ( filename.c_str() );
   string from;
   string to;
   double weight;
   
   while ( getline(inf,from,'\t') )
   {     
         getline(inf,to,'\t');
         inf>>weight;  
         if(inf.good())
         {          
         Node *Target = node_map.find_in_nodemap ( to );
         Node *Source = node_map.find_in_nodemap ( from );
         Edge *connector = new Edge ( weight, Target );
         Source->neighbors.push_back ( connector );
         }
   }
}
int main()
{
   NodeMap nodes;
   ifstream inf;
   string filename;
   cout << "Enter filename : ";
   cin >> filename;
   inf.open ( filename.c_str() );
   get_graph ( filename, nodes );

   string s;
   cout << "Enter start city : ";
   cin >> s;
   dijkstra ( s, nodes );

   system ( "PAUSE" );
   return 0;
}
Sydney NewYork	1300
NewYork Paris 1200
London Sydney 1100
Sydney London 1300
Paris NewYork 900
NewYork London 700
London Paris 200
Sydney Paris 900
Paris London 300
Brussels Paris 200
London Brussels 300
Sydney Brussels 700
Brussels NewYork 400
Paris Brussels 100
Tokyo NewYork 500
Tokyo Paris 600
Sydney Tokyo 300
Sydney Melbourne 200
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by daviddoria
0

Oops sorry dont know y but it only works like that when i type Sydney rest city says "Press any key to continue" right away!!!

plz help me with this first...

0

If possible, can you extract the problematic part of the code and post it so we don't have to download a file? Maybe you can make a very small example that demonstrates the problem without having the overhead of the rest of the code.

Dave

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.