Need Help in Problem ->Longest Flight Route — CSES Problem set .

Правка en1, от Serioton_codes, 2024-08-08 04:39:21

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;
}
Теги cses graph, graph, dp

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Serioton_codes 2024-08-08 04:39:21 1603 Initial revision (published)