USACO 2023 February Contest, Silver Problem 3. Moo Route II

Checklist

Make sure to read all of How to ask for help on a problem before asking for help.

Problem Info

USACO 2023 February Contest, Silver Problem 3. Moo Route II
Link: https://usaco.org/index.php?page=viewproblem2&cpid=1304

What I’ve Tried

I’ve downloaded the test data and I really cannot find where the error is. I’ve tried creating my own test cases, and my code seems to work as I want it to. My code passes test cases 1,2,5,11,12,13. All the test cases besides 1 and 2 are all very large.

My Work

#include<bits/stdc++.h>

using namespace std;

using ll = long long;
#define mp(a,b) make_pair(a,b)

struct Node{ // for every flight, this is stored to the city the bessie leaves
    ll leaveTime, arriveTime; // when bessie leaves, when bessie arrives at the next city
    int to; // what city bessie arrives to
};

vector<vector<Node>> adj;
vector<ll> layover; // stores the layover times
vector<ll> ans; 
vector<set<int>> visited; // stores all the cities that go to a city. whenever we visit a city, we erase the city it came from so we don't infinite loop

void dfs(int i, int arriveFrom=-1, ll arriveTime=-1){ // if i=0 is a bit of a special case. 
    if(arriveFrom!=-1) if(visited[i].find(arriveFrom)==visited[i].end()) return; // if youve already visited from this city before
    // printf("i=%d, arriveTime = %lld, layoverTime = %lld\n",i,arriveTime, layover[i]);
    ll minLeaveTime=0;
    if(arriveTime!=-1){ // just making sure i!=0. 
        ans[i] = min(ans[i], arriveTime);
        minLeaveTime = arriveTime+layover[i];
    }
    visited[i].erase(arriveFrom);
    for(Node e: adj[i]){
        // printf("start: %d, destination: %d, leaveTime: %lld, arriveTime: %lld, minLeaveTime: %lld\n", i, e.to, e.leaveTime, e.arriveTime, minLeaveTime);
        if(e.leaveTime<minLeaveTime || e.to == 0) continue; // either you don't have enough time because of layover or you're going to 0 which there is no point of.
        // cout<<"going\n";
        dfs(e.to, i, e.arriveTime);
    }
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    layover.resize(n);
    adj.resize(n);
    ans.resize(n,INT64_MAX);
    ans[0]=0;
    visited.resize(n);
    for(int i=0;i<m;i++){
        int c,d;
        ll r,s;
        cin>>c>>r>>d>>s;
        c--; d--;
        adj[c].push_back({r,s,d});
        visited[d].insert(c);
    }
    for(int i=0;i<n;i++) cin>> layover[i];

    dfs(0);

    for(auto e: ans){
        if(e==INT64_MAX){
            cout<<"-1\n";
        }else{
            cout<<e<<'\n';
        }
    }

}

Add explanation of code here
Basically the idea is that you just dfs the graph and connect nodes through the flights. Theres a posibility you can’t go to an adjacent node if the layover time makes it impossible
The only reason we would want to revisit and airport is if the new arriveTime is possibly less than the previous one, allowing more flights out of this airport and also decreasing the min time to get to that city. That’s what the visited is for.

Any help would be greatly appreciated and I’ll try my best to answer questions.