As usual Dijkstra but getting TLE- can someone please tell the reason? Thanks!


#1
#define vi vector<int>
 
bool srt(vector<int> &a, vector<int> &b)
{
    return a[1]<b[1];
}
 
int Solution::solve(int n, vector<vector<int> > &e, int s, int d, vector<vector<int> > &ee) 
{
    unordered_map<int,vector<vector<int>>>adj;
    
   
    //vector<int> ans;
   int ans=INT_MAX;
    
    for(int i=0;i<e.size();i++)
    {
        adj[e[i][0]].push_back({e[i][1],e[i][2]});
        adj[e[i][1]].push_back({e[i][0],e[i][2]});
    }
    
    
    
    for(int i=0;i<ee.size();i++)
    {
        priority_queue<vi, vector<vi>, function<bool(vi &,vi &)>>q(srt);
    
         vector<int>cost(n+1,INT_MAX);
         unordered_map<int,vector<vector<int>>>adj_ex;
         
        adj_ex = adj;
        if(ee[i][0]<=n && ee[i][1]<=n)
        {
            adj_ex[ee[i][0]].push_back({ee[i][1],ee[i][2]});
            adj_ex[ee[i][1]].push_back({ee[i][0],ee[i][2]});
        }
        else
            continue;
        
    
        q.push({0,s});
        cost[s]=0;//as start
        
        while(!q.empty())
        {
            auto t=q.top();
            q.pop();
        
            int cv = t[0];
            int v=t[1];
            
            //dont check while visiting neigh as that is not the final time 
            //it can be relaxed later= final time is only when popped- Karthik
            if(v==d)
            {
                ans = min(ans,cv);
               // ans.push_back(cv);
                break;
            }
                
            
            for(int j=0;j<adj_ex[v].size();j++)
            {
                int u = adj_ex[v][j][0];
                int cc=  adj_ex[v][j][1];
                
                if(cost[u]>cost[v]+cc)
                {
                    cost[u] = cost[v]+cc;
                    q.push({cost[u],u});
                }
                
            }
        }
    }
    
   /* if(ans.size()==0)
        return -1;
    
    return *min_element(ans.begin(),ans.end());*/
    
    
    if(ans==INT_MAX)
        return -1;
    return ans;
}