This page is still under construction.

Parts of this page are still being built. What you see may change.

The Loathesome Hay Baler

Interview

Time limit1sMemory limit128 MB

Summary
Rollers touch when center distance equals the sum of radii; find the path from the drive roller to the take-off roller and sum the absolute speeds, truncated.
Level

Medium5 of 10

Topics
Graph, DFS, Geometry, Math
Solved
No attempts yet

Problem

A hay baler is powered by NN rollers (2≤N≤10502 \le N \le 1050). Instead of a single drive shaft, the rollers press against one another, so a spinning roller turns every roller it touches.

Each roller ii is centered at (Xi,Yi)(X_i, Y_i) (−5000≤Xi,Yi≤5000-5000 \le X_i, Y_i \le 5000) and has radius RiR_i (3≤Ri≤8003 \le R_i \le 800). Two rollers touch, and therefore transmit power, exactly when the distance between their centers equals the sum of their radii. All coordinates and radii are integers, so this test is exact.

The drive-roller is the roller centered at (0,0)(0, 0). It turns clockwise at 10000 revolutions per hour (rph). The power take-off is the roller centered at (Xt,Yt)(X_t, Y_t).

When a roller of radius RdR_d spinning at SS rph drives a roller of radius RxR_x, the driven roller turns at −S⋅Rd/Rx-S \cdot R_d / R_x rph. The sign flips because two meshing rollers spin in opposite directions.

Every roller other than the drive-roller is driven by exactly one other roller, so the contacts between rollers form a tree rooted at the drive-roller, and there is a single path of rollers from the drive-roller to the power take-off. Only the rollers on that path (the drive-roller, the take-off roller, and every roller between them) belong to the power-train; all other rollers are ignored.

Compute the sum of the absolute values of the speeds (in rph) of every roller on the power-train, then output that sum truncated to an integer.

Input

  • Line 1: three space-separated integers NN, XtX_t, YtY_t.
  • Lines 2 to N+1N+1: line i+1i+1 contains three space-separated integers XiX_i, YiY_i, RiR_i describing roller ii.

Output

  • A single line containing one integer: the sum of the absolute values of the speeds of all rollers on the power-train (the drive-roller, every driven roller along the way, and the power take-off roller), truncated to an integer.

Hint

Because each driven roller multiplies the previous speed by Rd/RxR_d/R_x, the magnitudes telescope: a roller of radius RR on the path spins with magnitude 10000⋅Rdrive/R10000 \cdot R_{\text{drive}} / R rph. For example, a drive-roller of radius 10 driving a radius-20 roller, which in turn drives another radius-20 roller, produces speeds of 10000, -5000, and 5000 rph, whose absolute values sum to 20000.

Examples3

  1. Example 1

    Input
    4 32 54
    0 0 10
    0 30 20
    32 54 20
    -40 30 20
    
    Expected output
    20000
    
  2. Example 2

    Input
    2 0 30
    0 0 10
    0 30 20
    
    Expected output
    15000
    
  3. Example 3

    Input
    3 17 0
    0 0 6
    10 0 4
    17 0 3
    
    Expected output
    45000