Created
April 2, 2016 12:02
-
-
Save codegagan/673f0969f3f4f379803ac67f160d1fbe to your computer and use it in GitHub Desktop.
HackerEarth Tour challenge: https://www.hackerearth.com/problem/algorithm/tour/?utm_campaign=user-activity-email&utm_medium=email&utm_source=user-recommend-problemdescription/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.math.BigInteger; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by gagan on 2/4/16. | |
*/ | |
public class Tour { | |
public static void main(String... args) throws Exception{ | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String line = br.readLine(); | |
BigInteger distance=new BigInteger("0"); | |
int distinctCities = Integer.parseInt(line); | |
Map<String,Integer> cities = new HashMap<>(); | |
for (int i = 0; i < distinctCities; i++) { | |
cities.put(br.readLine(),i+1); | |
} | |
BigInteger[][] distanceMatrix = new BigInteger[distinctCities][distinctCities]; | |
for (int i = 0; i < distinctCities; i++) { | |
line =br.readLine(); | |
int j =0; | |
for(String column: line.split(" ")){ | |
distanceMatrix[i][j]=new BigInteger(column); | |
j++; | |
} | |
} | |
int visits =Integer.parseInt(br.readLine()); | |
int lastCityIndex=1; | |
for(int i = 0; i < visits; i++){ | |
String city =br.readLine(); | |
int visitingCityIndex = cities.get(city); | |
distance = distance.add(distanceMatrix[lastCityIndex-1][visitingCityIndex-1]); | |
lastCityIndex=visitingCityIndex; | |
} | |
System.out.println(distance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment