This is an old post. So, I hope it is not too late to answer. I have come up with a solution for the fly & trains problem that allows for the trains to travel at different speeds and for the fly to travel at different speeds depending on its direction, and which leads to the "short cut" solution in the simplest cases. This solution is also computationally simple.
Assume that the trains start a distance d apart, that the train the fly is on initially (the "s-train") travels at speed s, while the other train (the "r-train") travels at speed r. Furthermore assume that when the fly flies from the s-train to the r-train it does so at speed f, and when it flies from the r-train to the s-train it does so at speed e. For reasonableness we must also assume that f>s and e>r.
I will refer to each flight of the fly from one train to another as a "leg" of its journey. By an "sr-leg" I mean one in which the fly goes from the s-train to the r-train, and by an "rs-leg" I mean one in which the fly goes from the r-train to the s-train.
For an sr-leg with initial inter-train distance D, the duration of that leg will be D/(f+r), the length of the leg (distance the fly travels) will be fD/(f+r), during which the s-train will have advanced sD/(f+r), and thus the inter-train distance of the next leg will be (f-s)D/(f+r).
Similarly, for an rs-leg with initial inter-train distance D, the duration of that leg will be D/(e+s), the length of that leg will be eD/(e+s), during which the r-train will have advanced rD/(e+s), and thus the inter-train distance of the next leg will be (e-r)D/(e+s).
Define the ratios of the lengths of successive legs,
a = (f-s)/(f+r),
b = (e-r)/(e+s),
and the ratios of the fly's (ground)speed to the relative speed of the fly and the train it is flying to,
g = f/(f+r),
h = e/(e+s).
Then it can be seen that the inter-train distances of successive legs is
d, da, dab, da^2b, da^2b^2, da^3b^2, da^3b^3, ...
and thus that the lengths of successive legs is
gd, hda, gdab, hda^2b, gda^2b^2, hda^3b^2, gda^3b^3, ...
So that the total distance the fly travels is
gd[1 + (ab) + (ab)^2 + (ab)^3 ...] + hda[1 + (ab) + (ab)^2 + (ab)^3 ...]
and since a<1 and b<1, and thus ab<1, the above equals
= d(g+ha)/(1-ab).
This is the general solution to the problem.
If we assume, per the original problem, that both trains travel at the same speed s, and that the fly always travels at speed f, the above expression reduces to fd/2s.
If we assume that the trains travel at different speeds s and r, but the fly always travels as speed f, the above expression reduces to fd/(r+s).
If we assume there is wind with speed w parallel to the track in the direction from the s-train to the r-train, so that the fly's groundspeed is f+w on the sr-legs and f-w on the rs-legs, and also assume that r=s, then the above expression reduces to (d/2s)(f + (s-w)(w/f)), which reduces to the solution of the original problem fd/2s when w=0, but also, interestingly, when w=s.
The general solution can be written as
d(2ef - es + fs) / ((e+f)(r+s))
or more symmetrically as

The term on the left d/(r+s) is the total time the fly travels (before the trains crash), while the term on the right is apparently the time-averaged speed of the fly.