1. Abridged Problem Statement  
Given a directed graph with N towns (vertices) and M one-way routes (edges). Each edge from u to v has a cost C and a time T. Find a simple directed cycle (length ≥2) that maximizes the ratio (sum of costs)/(sum of times). If no cycle exists, output 0. Otherwise, output the number of towns in the cycle followed by the sequence of towns in order.

2. Detailed Editorial  

Goal  
We want a cycle G that maximizes  
    F(G) = (Σ cost on G)/(Σ time on G).  

Observation  
This is the classic “maximum mean weight cycle” problem on a directed graph, where each edge e has weight w(e) = cost(e) - λ·time(e). If, for a given λ, there is a cycle whose total modified weight is positive, then its original cost/time ratio exceeds λ. We can binary‐search λ and detect positive‐weight cycles in the modified graph.

Reformulation  
Define for any candidate ratio x:  
    modified_weight(e) = cost(e) - x·time(e).  
A cycle has average cost/time > x exactly when the sum of modified_weight over that cycle is > 0.

Cycle detection by Bellman-Ford  
To test whether any positive‐weight cycle exists, we can negate the weights and run standard Bellman‐Ford to detect negative cycles. Concretely we set  
    w'(e) = x·time(e) - cost(e),  
so a negative cycle in w' corresponds to a cycle of mean cost/time > x.

Algorithm Steps  
1. Read n, m and all edges (u, v, cost, time).  
2. Binary‐search x over an interval that surely contains the optimum ratio (e.g., [0, 200]).  
3. For each mid = (lo + hi)/2:  
   a. Build w'(e) = mid·time(e) - cost(e).  
   b. Add a super‐source connected to every node with zero‐weight edges.  
   c. Run Bellman‐Ford for n iterations; if any distance can still be relaxed in the nth pass, a negative cycle exists. Record parent pointers.  
   d. If a negative cycle exists, set lo = mid; else hi = mid.  
4. After ~60–100 iterations, lo≈max ratio. Re‐run the Bellman‐Ford check at x=lo to recover one negative cycle.  
5. Extract the cycle by following parent pointers n steps from one node on the detected cycle to ensure landing inside the cycle, then traverse parents until we close the loop.  
6. Output the cycle’s size and the sequence of towns. If none found, output 0.

Complexity  
Binary‐search iterations: ~100.  
Each Bellman‐Ford: O(n·m).  
Total: O(100·n·m), which is fine for n≤50, m≤2500.

3. Provided C++ Solution with Detailed Comments  

#include <algorithm>  
#include <iostream>  
#include <vector>  
using namespace std;

// Structure to hold an edge: from u to v, cost c, time t  
struct Edge {  
    int u, v, c, t;  
};

int n, m;                     // Number of towns (vertices) and routes (edges)  
vector<Edge> edges;           // Edge list  
vector<double> dist;          // Distances (for Bellman-Ford)  
vector<int> par;              // Parent pointers to reconstruct paths

// check(x): returns (has_positive_cycle, one_cycle) for modified weights w' = x·t - c  
pair<bool, vector<int>> check(double x) {
    // Initialize distances to +infinity, parent to -1  
    dist.assign(n + 2, 1e18);  
    par.assign(n + 2, -1);

    int source = n + 1;  
    dist[source] = 0;            // Distance from super-source to itself is 0

    // We imagine edges from super-source to every node with weight = 0  
    // We can simulate that by allowing relaxation from source every iteration.

    // Perform n iterations of relaxation  
    for(int i = 0; i < n; i++) {
        // Relax all real edges  
        for(auto& e: edges) {
            double w = e.t * x - e.c;         // Modified weight
            if(dist[e.u] < 1e17 && dist[e.u] + w < dist[e.v]) {
                dist[e.v] = dist[e.u] + w;    // Update distance
                par[e.v] = e.u;               // Remember parent
            }
        }
        // Also relax from super-source to every node
        for(int v = 1; v <= n; v++) {
            if(dist[source] < dist[v]) {
                dist[v] = dist[source];
                par[v] = source;              // Parent is super-source
            }
        }
    }

    // One more pass to detect negative cycle  
    int cycle_node = -1;
    for(auto& e: edges) {
        double w = e.t * x - e.c;
        if(dist[e.u] < 1e17 && dist[e.u] + w < dist[e.v]) {
            cycle_node = e.v;               // Found a node that can still relax
            break;
        }
    }

    if(cycle_node == -1) {
        // No negative cycle  
        return {false, {}};
    }

    // To ensure we land inside the cycle, walk back n steps from cycle_node  
    for(int i = 0; i < n; i++) {
        cycle_node = par[cycle_node];
    }

    // Reconstruct the cycle by following parents until we return to cycle_node  
    vector<int> cycle;
    int curr = cycle_node;
    do {
        cycle.push_back(curr);
        curr = par[curr];
    } while(curr != cycle_node);

    reverse(cycle.begin(), cycle.end());  // Optional: put cycle in forward order  
    return {true, cycle};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Read input  
    cin >> n >> m;
    edges.resize(m);
    for(int i = 0; i < m; i++) {
        cin >> edges[i].u >> edges[i].v >> edges[i].c >> edges[i].t;
    }

    // Binary search bounds for ratio  
    double lo = 0.0, hi = 200.0;
    for(int it = 0; it < 100; it++) {
        double mid = (lo + hi) / 2;
        auto result = check(mid);
        if(result.first) {
            // We found a cycle whose avg cost/time > mid  
            lo = mid;
        } else {
            hi = mid;
        }
    }

    // Recover actual cycle at x = lo  
    auto final_res = check(lo);
    if(!final_res.first) {
        cout << "0\n";       // No cycle at all  
    } else {
        auto& cycle = final_res.second;
        cout << cycle.size() << "\n";
        for(int i = 0; i < cycle.size(); i++) {
            if(i) cout << " ";
            cout << cycle[i];
        }
        cout << "\n";
    }

    return 0;
}

4. Python Solution with Detailed Comments  

```python
import sys
sys.setrecursionlimit(10**7)

def readints():
    return map(int, sys.stdin.readline().split())

def find_cycle(n, edges, x):
    """
    Run Bellman-Ford to detect a negative cycle under weights w' = x*t - c.
    Returns (has_cycle, list_of_nodes_in_cycle).
    """
    INF = 1e18
    dist = [INF] * (n + 2)
    par  = [-1]  * (n + 2)
    source = n + 1
    dist[source] = 0.0

    # Relax edges n times
    for _ in range(n):
        # Relax real edges
        for u, v, c, t in edges:
            w = x*t - c
            if dist[u] + w < dist[v]:
                dist[v] = dist[u] + w
                par[v] = u
        # Relax super-source → every node
        for v in range(1, n+1):
            if dist[source] < dist[v]:
                dist[v] = dist[source]
                par[v] = source

    # One extra pass to detect a negative cycle
    cycle_node = -1
    for u, v, c, t in edges:
        w = x*t - c
        if dist[u] + w < dist[v]:
            cycle_node = v
            break

    if cycle_node == -1:
        return False, []

    # Walk back n steps to ensure inside the cycle
    for _ in range(n):
        cycle_node = par[cycle_node]

    # Reconstruct cycle
    cycle = []
    curr = cycle_node
    while True:
        cycle.append(curr)
        curr = par[curr]
        if curr == cycle_node:
            break
    cycle.reverse()
    return True, cycle

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    n, m = map(int, (next(it), next(it)))
    edges = []
    for _ in range(m):
        u, v, c, t = map(int, (next(it), next(it), next(it), next(it)))
        edges.append((u, v, c, t))

    lo, hi = 0.0, 200.0
    # Binary search for ~80–100 iterations
    for _ in range(80):
        mid = (lo + hi) / 2.0
        has, _ = find_cycle(n, edges, mid)
        if has:
            lo = mid
        else:
            hi = mid

    # Recover the cycle at best ratio lo
    has, cycle = find_cycle(n, edges, lo)
    if not has:
        print(0)
    else:
        print(len(cycle))
        print(" ".join(map(str, cycle)))

if __name__ == "__main__":
    main()
```

5. Compressed Editorial  

- We seek a directed cycle maximizing (∑cost)/(∑time).  
- Equivalently, for λ, check if there’s a cycle with ∑(cost − λ·time) > 0.  
- Negate to weights w' = λ·time − cost; detect negative cycles by Bellman‐Ford.  
- Binary‐search λ in [0,200] to precision.  
- On finding a negative cycle at final λ, reconstruct it using parent pointers.