Rishabh__a's blog

By Rishabh__a, 4 hours ago, In English

Hey, I was solving this problem — D. Minimum maximum on the Path

I have figured out what needs to be done, I think. But I am getting a WA. Here is my code —

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

vector<ll> a;
vector<ll> b;
vector<ll> c;
vector<vector<int>> adj;
ll n,m,d;


void printPath( vector<ll>& par, vector<ll> &temp ){
    int i=n;
    while(par[i]!=i){
        temp.push_back(i);
        i = par[i];
    }
    temp.push_back(1);
    reverse( temp.begin(), temp.end());
}


bool check(ll mid, vector<ll> & temp){
    vector<bool> vis(n+1, false);
    vector<ll> par(n+1,0);

    par[1]=1;
    bool found = false;
    queue<int> Q;
    Q.push(1);
    vis[1]=true;

    int dist =0;

    while(!Q.empty()){
        int k = Q.size();
        dist++;
        if(dist>d+1) return false;
        while(k--){
            int node = Q.front();
            if(node==n){
                found = true;
                break;
            } 
            Q.pop();

            for(auto nei : adj[node]){
                if(!vis[nei] && c[nei]<=mid){
                    Q.push(nei);
                    par[nei] =node;
                    vis[nei] = true;
                }
            }

        }
        if(found) break;
    }   

    
    if(vis[n]){
        printPath(par,temp);
        return true;
    }
    return false;
}

void solve(){
    cin>>n>>m>>d;
    a.resize(m);
    b.resize(m);
    c.resize(m);
    adj.resize(n+1);


    for(int i=0;i<m;i++){
        cin>>a[i]>>b[i]>>c[i];
    }
    a.insert(a.begin(),0);
    b.insert(b.begin(),0);
    c.insert(c.begin(),0);
    
    for(int i=1;i<=m;i++){
        adj[a[i]].push_back(b[i]);
    }

    ll l=0; 
    ll r = 1e11;
    vector<ll> ans;

    while(r>l+1){
        ll mid = (l+r)/2;
        vector<ll> temp;
        if(check(mid, temp)){
            ans=temp;
            r  = mid;
        }
        else{
            l =  mid;
        }
    }

    if(ans.size()==0){
        cout<<-1<<"\n";
        return;
    }

    cout<<ans.size()-1<<"\n";

    for(auto x:ans){
        cout<<x<<" ";
    }
    cout<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();    
    return 0;
}

What is the problem here? Is the level order(bfs) wrong? Any help is appreciated, thanks.

  • Vote: I like it
  • 0
  • Vote: I do not like it