Loading...
Find a starting station to complete the tour. Reset start whenever the running surplus becomes negative. If total gas ≥ total cost, the solution exists.
1procedure GAS_STATION(gas, cost)2 start ← 0; surplus ← 0; total ← 03 for i from 0 to n-1 do4 diff ← gas[i] − cost[i]5 surplus ← surplus + diff; total ← total + diff6 if surplus < 0 then7 start ← i + 1; surplus ← 08 end if9 end for10 if total ≥ 0 return start else return −111end procedure