Hi,
I'm having a similar issue. I'm not sure where exactly my logic is flawed, but, I have a feeling that it is flawed in a few areas. I read over the previous posts, and tried implementing a 'standard' Dijkstra's style solution, however, I am getting wacky results...any feedback would be much appreciated!
Code: Select all
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int INFINITY = 9e8;
// First, compare based on the cost of getting to each node
// Next, compare based on the location of each node
// Finally, compare based on the 'riding the bike' state
bool operator<( const pair < pair <int, int>, bool>& p1, const pair < pair <int, int>, bool>& p2 )
{
if( p1.first.first != p2.first.first ) return p1.first.first < p2.first.first;
if( p1.first.second != p2.first.second ) return p1.first.second < p2.first.second;
return p1.second < p2.second;
}
int main()
{
int test_case_number = 1, num_nodes, number_edges;
while( cin >> num_nodes >> number_edges )
{
int starting_node = 0, ending_node = num_nodes - 1;
pair <int, int> temp_pair;
vector < pair <int, int > > temp_vector_of_pairs( 0, temp_pair );
vector < vector < pair <int, int> > > adj_list( num_nodes, temp_vector_of_pairs );
int from, to, cost;
for( int edge_number = 0; edge_number < number_edges; edge_number++ )
{
cin >> from >> to >> cost;
adj_list[from].push_back( make_pair( to, cost ) );
}
// Distance from the starting node to every other node
// The second parameter represents whether or not Shoan is 'riding the bike' (true = riding the bike, false = walking)
vector <int> tmp_distance_column( 2, INFINITY );
vector < vector <int> > distances( num_nodes, tmp_distance_column );
distances[starting_node][false] = 0;
// I am not really sure of the correct way to use this:
// priority_queue < pair < pair <int, int>, bool >, vector < pair < pair <int, int>, bool > >, greater < pair < pair <int, int>, bool > > > pq;
priority_queue < pair < pair <int, int>, bool > > pq;
pq.push( make_pair( make_pair( 0, starting_node ), false ) );
// Here, I am trying to use the standard-ish way of implementing Dijkstra's
// This is heavily sampled from the Topcoder Algorithm Tutorial "STL Part 2"
while( !pq.empty() )
{
int vertex1 = pq.top().first.second, distance1 = pq.top().first.first;
bool riding_bike = pq.top().second;
pq.pop();
if( vertex1 == ending_node ) break;
if( distance1 <= distances[vertex1][riding_bike] )
{
vector < pair <int, int> >::iterator it = adj_list[vertex1].begin();
while( it != adj_list[vertex1].end() )
{
int vertex2 = it->first, distance2= it->second;
if( distances[vertex2][!riding_bike] > distances[vertex1][riding_bike] + distance2 )
{
distances[vertex2][!riding_bike] = distances[vertex1][riding_bike] + distance2;
pq.push( make_pair( make_pair( distances[vertex2][!riding_bike], vertex2), !riding_bike) );
}
it++;
}
}
}
cout << "Set #" << test_case_number << endl;
if( distances[ending_node][true] == INFINITY ) cout << "?" << endl;
else cout << distances[ending_node][true] << endl;
test_case_number++;
pq = priority_queue < pair < pair <int, int>, bool > >();
adj_list.clear();
distances.clear();
}
return 0;
}