We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 13
  5. Jack goes to Rapture

Jack goes to Rapture

Problem
Submissions
Leaderboard
Discussions
Editorial
HackerRank Logo
|
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 13
  5. Jack goes to Rapture
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

Jack has just moved to a new city called Rapture. He wants to use the public public transport system. The fare rules are as follows:

  1. Each pair of connected stations has a fare assigned to it regardless of direction of travel.
  2. If Jack travels from station A to station B, he only has to pay the difference between (the fare from A to B) and (the cumulative fare paid to reach station A), [fare(A,B) - total fare to reach station A]. If the difference is negative, travel is free of cost from A to B.

Jack is low on cash and needs your help to figure out the most cost efficient way to go from the first station to the last station. Given the number of stations (numbered from to ), and the fares (weights) between the pairs of stations that are connected, determine the lowest fare from station to station .

Example



The graph looks like this:

image
Travel from station costs for the first segment () then the cost differential, an additional for the remainder. The total cost is .

Travel from station costs for the first segment, then an additional for the remainder, a total cost of .

The lower priced option costs .

Function Description
Complete the getCost function in the editor below.

getCost has the following parameters:

  • int g_nodes: the number of stations in the network
  • int g_from[g_edges]: end stations of a bidirectional connection
  • int g_to[g_edges]: is connected to at cost
  • int g_weight[g_edges]: the cost of travel between associated stations

Prints
- int or string: the cost of the lowest priced route from station to station or NO PATH EXISTS. No return value is expected.

Input Format

The first line contains two space-separated integers, and , the number of stations and the number of connections between them.
Each of the next lines contains three space-separated integers, and , the connected stations and the fare between them.

Constraints

Sample Input 1

CopyDownload
5 5
1 2 60
3 5 70
1 4 120
4 5 150
2 3 80

Sample Output 1

80

Sample Input 2

CopyDownload
5 6
1 2 30
2 3 50
3 4 70
4 5 90
1 3 70
3 5 85

Sample Output 2

85

  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website