Wednesday, August 24, 2016

UVA 383 - Shipping Routes - Solution

Unknown
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
class Graph{
    int V;
    list<int>*adj;
public:
    Graph(int V);
    ~Graph();
    void addEdge(int u, int v);
    int BFS(int s,int dest);
};
Graph::Graph(int V) //construtor
{
    this->V=V;
    adj=new list<int >[V];
}
Graph::~Graph() //destructor
{
    delete []adj;
}
void Graph::addEdge(int u,int v)
{
    adj[u].pb(v);
}
int Graph::BFS(int s,int dest)
{
    bool visited[V];
    int level[V];
    memset(visited,false,sizeof(visited));
    list<int>q;
    list<int>::iterator i;
    q.pb(s);
    visited[s]=true;
    level[s]=0;
    while (!q.empty())
    {
        s=q.front();
        q.pop_front();
        for (i=adj[s].begin();i!=adj[s].end();++i)
        {
            if (!visited[*i])
            {
                q.pb(*i);
                visited[*i]=true;
                level[*i]=level[s]+1;
                if (*i==dest) return level[*i];
            }
        }
    }
    return -1;
}
int main()
{
    int tc,c=1;
    scanf("%d",&tc);
    printf("SHIPPING ROUTES OUTPUT\n");
    while (tc--)
    {
        printf("\nDATA SET  %d\n\n",c++);
        int m,n,p;
        scanf("%d%d%d",&m,&n,&p);
        Graph g(m);
        map<string,int>mp;
        string s,s1;
        getchar();
        for (int i=0;i<m;i++)
        {
            cin>>s;
            mp[s]=i;
        }
        while (n--)
        {
            cin>>s>>s1;
            g.addEdge(mp[s],mp[s1]);
            g.addEdge(mp[s1],mp[s]);
        }
        
        while (p--)
        {
            int a;
            scanf("%d\n",&a);
            cin>>s>>s1;
            int l=g.BFS(mp[s],mp[s1]);
            if (l==-1) printf("NO SHIPMENT POSSIBLE\n");
            else printf("$%d\n",a*l*100);
        }
    }
    printf("\nEND OF OUTPUT\n");
    return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,