Friday, March 10, 2017

Solution of UVa 10048 Audiophobia

/*  Bismillahir Rahmanir Rahim
    Solution-Using BFS
*/
#include<bits/stdc++.h>
#define fi(n, m) for(int i=n; i<=m; i++)
#define fd(n, m) for(int i=n; i>=m; i--)
#define inf  100009
using namespace std;
vector<int>vt[105], cost[105];
int dis[105], zz;

void bfs(int st){
    queue<int>q;
    q.push(st);
    dis[st]=0;
    int p, sz, temp, xx;
    while(!q.empty()){
        p=q.front();
        q.pop();
        sz=vt[p].size();
        fi(0, sz-1){
            xx=vt[p][i];
            temp=max(dis[p], cost[p][i]);
            if(temp<dis[xx]){
                dis[xx]=temp;
                q.push(xx);
            }
        }
    }
}

int main(){
    int t, cs=1, n, m, u, v, w, tt, q, ans;
    while(1){
        cin>>n>>m>>q;
        if(n==0&&m==0&&q==0)break;
         if(cs!=1)cout<<endl;
        fi(0, m-1){
            cin>>u>>v>>w;
            vt[u].push_back(v);
            vt[v].push_back(u);
            cost[u].push_back(w);
            cost[v].push_back(w);
        }
        cout<<"Case #"<<cs++<<endl;
        while(q--){
            fi(1, n) dis[i]=inf;
            cin>>tt>>zz;
            bfs(tt);
            if(dis[zz]==inf)cout<<"no path"<<endl;
            else cout<<dis[zz]<<endl;
        }
        fi(0, n){
            vt[i].clear();
            cost[i].clear();
        }
    }
    return 0;
}

No comments:

Post a Comment