I am not able to find why the code is giving tle for one case??

Revision en2, by ompd292, 2023-07-25 21:14:33

https://cses.fi/problemset/task/1680/

#include <bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  vector<pair<int,int>>adj[n+1];
  for(int i=0;i<m;i++){
    int u,v,wt;
    cin>>u>>v;
    adj[u].push_back({v,1});
  }
  vector<int>dist(n+1,INT_MIN);
  vector<int>par(n+1,-1);
  dist[1]=0;
  priority_queue<pair<int,int>>pq;
  pq.push({0,1});
  while(!pq.empty()){
    int node=pq.top().second;
    int dis=pq.top().first;
    pq.pop();
    for(auto it:adj[node]){
      int adjnode=it.first;
      int wt=it.second;
      
      if(dist[adjnode]<dis+wt){
        dist[adjnode]=dis+wt;
        par[adjnode]=node;
        pq.push({dist[adjnode],adjnode});
      }
    }
  }
  if(dist[n]!=INT_MIN){
  cout<<dist[n]+1<<endl;
  int node=n;
  vector<int>v;
  while(node!=-1){
    v.push_back(node);
    node=par[node];
  }
  reverse(v.begin(),v.end());
  for(int i=0;i<v.size();i++)
  cout<<v[i]<<" ";
  }
  else
  cout<<"IMPOSSIBLE";
  return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ompd292 2023-07-25 21:14:33 27
en1 English ompd292 2023-07-25 21:08:39 1082 Initial revision (published)