fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct PetrolPumpData
  5. {
  6. int petrol;
  7. int distance;
  8. };
  9.  
  10. /*It returns -1 if there is no possible tour*/
  11. int getStartPointOfTour(struct PetrolPumpData arr[], int n)
  12. {
  13. int start_point = 0;
  14. int end_point = 1;
  15.  
  16. int curr_petrol = arr[start_point].petrol - arr[start_point].distance;
  17.  
  18. while (end_point != start_point || curr_petrol < 0)
  19. {
  20. /*If the curr_petrol becomes less than zero then remove the starting point of the pump tour*/
  21. while (curr_petrol < 0 && start_point != end_point)
  22. {
  23. curr_petrol = curr_petrol - (arr[start_point].petrol - arr[start_point].distance);
  24. start_point = (start_point + 1)%n;
  25. if (start_point == 0)
  26. return -1;
  27. }
  28. /*Add petrol pump data to current petrol*/
  29. curr_petrol = curr_petrol + arr[end_point].petrol - arr[end_point].distance;
  30. end_point = (end_point + 1)%n;
  31. }
  32. return start_point;
  33. }
  34.  
  35. int main()
  36. {
  37. struct PetrolPumpData arr[] = {{4, 6}, {6, 5}, {7, 3}, {4, 5}};
  38. int result = getStartPointOfTour(arr,4);
  39. if(result == -1)
  40. printf("No Possible tour\n");
  41. else
  42. printf("start point of the tour is %d\n", result);
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
start point of the tour is 1