UVa : 762 (We Ship Cheap)



// http://uva.onlinejudge.org/external/7/762.html
// Runtime: 0.120s
// Tag: BFS, Path, Parent


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define pi acos(-1.0)
#define N 1000
#define LL long long

#define For(i, a, b) for( int i = (a); i < (b); i++ )
#define Fors(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fore(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Set(a, s) memset(a, s, sizeof (a))
#define Read(r) freopen(r, "r", stdin)
#define Write(w) freopen(w, "w", stdout)

using namespace std;

map <string, int> cityIndex;
vector <int> matrix [N];
int hop_count [N];
int parent [N];

void reset ()
{
    cityIndex.clear ();

    for ( int i = 0; i < N; i++ ) {
        matrix [i].clear ();
        hop_count [i] = INF_MAX;
        parent [i] = i;
    }
}

void bfs (int s, int t)
{
    queue <int> q;
    q.push (s);

    hop_count [s] = 0;

    while ( !q.empty () ) {
        int p = q.front (); q.pop ();

        if ( p == t ) return;

        Fors (i, matrix [p]) {
            if ( hop_count [matrix [p] [i]] == INF_MAX ) {
                parent [matrix [p] [i]] = p;
                hop_count [matrix [p] [i]] = hop_count [p] + 1;
                q.push (matrix [p] [i]);
            }
        }
    }
}

void print_path (int t)
{
    vector <int> path;
    path.push_back (t);

    while ( parent [t] != t ) {
        t = parent [t];
        path.push_back (t);
    }

    reverse (path.begin (), path.end ());

    string str [N];

    Fore (it, cityIndex) {
        str [(*it).second] = (*it).first;
    }

    for ( size_t i = 1; i < path.size (); i++ ) cout << str [path [i - 1]] << " " << str [path [i]] << endl;
}

int main ()
{
    //Read ("in.in");
    //Write ("out.out");

    int number_of_links;
    bool blank = false;

    while ( scanf ("%d", &number_of_links) != EOF ) {
        reset ();

        char city1 [5], city2 [5];
        int cityId = 1;
        for ( int i = 1; i <= number_of_links; i++ ) {
            scanf ("%s %s", city1, city2);
            if ( !cityIndex [city1] ) cityIndex [city1] = cityId++;
            if ( !cityIndex [city2] ) cityIndex [city2] = cityId++;

            matrix [cityIndex [city1]].push_back (cityIndex [city2]);
            matrix [cityIndex [city2]].push_back (cityIndex [city1]);
        }

        scanf ("%s %s", city1, city2);
        bfs (cityIndex [city1], cityIndex [city2]);

        if ( blank ) printf ("\n"); blank = true;

        if ( !cityIndex [city1] || !cityIndex [city2] || hop_count [cityIndex [city2]] == INF_MAX ) printf ("No route\n");
        else print_path (cityIndex [city2]);

    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s