Uva 383 - Shipping Routes

#include<bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define psb push_back
#define fast ios_base::sync_with_stdio(false)

const int high = 105;
const int red = 1;
const int blue = 0;
const int white = -1;
bool bipartite = true;

typedef map<string, int>mpsi;

vector<int>adj[high];
int cost[high], visited[high];
mpsi mp;

void CLR()
{
    for(int i=0; i<high; i++)
    {
        adj[i].clear();
    }

    mp.clear();
}

void CLR_cost()
{
    for(int i=0; i<high; i++)
    {
        cost[i] = 0;
        visited[i] = 0;
    }
}

void BFS(int src, int des)
{
    visited[src] = 1;
    cost[src] = 0;

    queue<int>Q;

    Q.push(src);

    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();

        if(u == des) return;

        int sze = adj[u].size();

        for(int i=0; i<sze; i++)
        {
            int v = adj[u][i];

            if(!visited[v])
            {
                visited[v] = 1;
                Q.push(v);
                cost[v] = cost[u] + 1;
            }
        }
    }

}

int main()
{
    fast;
    int test , i , tc=0;
    int M, N , P, num=0, ship_size=0;
    string x , y;
    cin >> test;
    cout << "SHIPPING ROUTES OUTPUT\n";

    while(test--)
    {
        CLR();
        CLR_cost();

        num = 0;

        cin >> M >> N >> P;

        for(i=0; i<M; i++)
        {
            cin >> x;

            if(mp[x] == 0)
            {
                num += 1;
                mp[x] = num;
            }

            //cout << mp[x] << "\n";
        }

        for(i=0; i<N; i++)
        {
            cin >> x >> y;

            //cout << mp[x] << " " << mp[y] << "\n";

            adj[mp[x]].psb(mp[y]);
            adj[mp[y]].psb(mp[x]);
        }

        cout << "\nDATA SET  " << ++tc << "\n\n";

        for(i=0; i<P; i++)
        {
            cin >> ship_size >> x >> y;

            CLR_cost();

            BFS(mp[x] , mp[y]);

            if(cost[mp[y]])
            {
                int ans = ship_size * cost[mp[y]] * 100;

                cout << "$" << ans << "\n";
            }

            else
            {
                cout << "NO SHIPMENT POSSIBLE\n";
            }
        }
    }

    cout << "\nEND OF OUTPUT\n";

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number