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

Revision en1, by ompd292, 2023-07-25 21:08:39

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

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}); } vectordist(n+1,INT_MIN); vectorpar(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; vectorv; 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)