Untitled

 avatar
unknown
plain_text
a month ago
1.4 kB
40
Indexable
class Solution {
public:
    int wt (int s,int d,vector<vector<pair<int,int>>>&vk){
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> pq;
        int n = vk.size();
        vector<int> f(n,INT_MAX);
        pq.push({0,s});
        f[s] = 0;
        while(!pq.empty()){
            auto it = pq.top(); pq.pop();
            if(it.first > f[it.second])continue;
            for(auto itk : vk[it.second]){
                if(f[itk.first] > it.first + itk.second){
                    f[itk.first] = it.first + itk.second;
                    pq.push({f[itk.first],itk.first});
                }
            }
        }
        return f[d];
    }
    int minimumThreshold(int n, vector<vector<int>>& edges, int source, int target, int k) {

        int st = 0,en = 1e9+1;
        int ans = en;
        while(st <= en){
            int thr = (en -st)/2 + st;
            vector<vector<pair<int,int>>> v(n+1);
            for(auto it : edges){
                int w = it[2] <= thr ? 0 : 1;
                v[it[0]].push_back({it[1],w});
                v[it[1]].push_back({it[0],w});
            }
            int fw = wt(source,target,v);
            if(fw <= k){
                ans = thr;
                en = thr -1;
            }
            else st = thr + 1;
        }
        if( ans >= 1e9+1)ans = -1;
        return ans;
        
    }
};
Editor is loading...
Leave a Comment