Serioton_codes's blog

By Serioton_codes, history, 5 months ago, In English

can someone help me out why my code is giving wrong answers for some test cases ? Following is my code-

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll INF = -1e16;
int n, m;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    vector<int> adj[n + 1];
    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
    }
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> p;
    p.push({0, 1});
    vector<int> vis(n + 1, 0), dist(n + 1, 1e9), parent(n + 1, -1);
    dist[1] = 0;
    while (!p.empty())
    {
        int u = p.top().second;
        int dis = p.top().first;
        p.pop();
        if (vis[u])
            continue;
 
        vis[u] = 1;
        for (auto adjnode : adj[u])
        {
            if (dist[adjnode] > dis - 1)
            {
                parent[adjnode] = u;
                dist[adjnode] = dis - 1;
                p.push({dis - 1, adjnode});
            }
        }
    }
 
    vector<int> ans;
    int x = n;
    while (parent[x] != -1)
    {
        ans.push_back(x);
        x = parent[x];
    }
    if (x != 1)
    {
        cout << "IMPOSSIBLE" << endl;
        return 0;
    }
    cout << ans.size() + 1 << endl;
    cout << 1 << " ";
 
    reverse(ans.begin(), ans.end());
 
    for (auto it : ans)
        cout << it << " ";
    cout << endl;
 
    return 0;
}

Full text and comments »

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