Sunday, August 17, 2014

uva : 383 - Shipping Routes


#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int test,y=1;
    scanf("%d",&test);
    printf("SHIPPING ROUTES OUTPUT\n");
    while(test--)
    {
        printf("\nDATA SET  %d\n\n",y++);
        int n,e,c;
        scanf("%d %d %d",&n,&e,&c);
        map<string,int>word;
        char a[100],b[100];
        for(int i=1;i<=n;i++)
        {
            scanf("%s",&a);
            word[a]=i;
        }
        vector<int>graph[n+1];
        for(int i=0;i<e;i++)
        {
            scanf("%s %s",&a,&b);
            int n1=word[a],n2=word[b];
            graph[n1].push_back(n2);
            graph[n2].push_back(n1);
        }
        for(int k=0;k<c;k++)
        {
            int cost;
            scanf("%d %s %s",&cost,&a,&b);
            int s=word[a],des=word[b];
            queue<int>q;
            q.push(s);
            int color[n+1],d[n+1];
            for(int i=1;i<=n;i++)color[i]=d[i]=0;
            color[s]=0;
            while(!q.empty())
            {
                int u=q.front();
                if(u==des)break;
                q.pop();
                for(int i=0;i<graph[u].size();i++)
                {
                    int v=graph[u][i];
                    if(color[v]==0)
                    {
                        color[v]=1;
                        d[v]=d[u]+1;
                        q.push(v);
                    }
                }
            }
            if(d[des])
            printf("$%d\n",d[des]*cost*100);
            else printf("NO SHIPMENT POSSIBLE\n");
        }
    }
    printf("\nEND OF OUTPUT\n");
    return 0;
}

No comments:

Post a Comment