Table of Contents

ExplanationImplementation

Official Analysis (C++)

Explanation

We can consider three possibilities:

  • Farmer John did not use any teleporter to transport the manure, so he travels a distance of ab|a - b|.
  • Farmer John travels to point xx from aa, teleports the manure to yy, then travels to bb, for a distance of ax+by|a - x| + |b - y|.
  • Farmer John travels to point yy from aa, teleports the manure to xx, then travels to bb, for a distance of ay+bx|a - y| + |b - x|.

We can calculate the minimum distance he needs to haul the manure using his tractor among the above possibilities.

Implementation

Time Complexity: O(1)\mathcal{O}(1)

#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("teleport.in", "r", stdin);
freopen("teleport.out", "w", stdout);
int a, b, c, d;
cin >> a >> b >> c >> d;
/*

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!