Walter White is on a tour to sell meth. There are N cities.
Each city has a id between 1 and N (both inclusive).
You are given a cost matrix.
In cost matrix, the element in the row denotes the cost of travelling between cities with i and j.
and
Given the path taken by Walter, print the cost of traveling.
Walter is at the city with 1 right now.
Example:
Input: n = 3, str = {"delhi", "bengaluru", "hyderabad"}, arr = {{0, 10, 20}, {10, 0, 55}, {20, 55, 0}}, q = 4, queries = {"bengaluru", "delhi", "hyderabad", "bengaluru"}
Output: 95
Approach
C++
#include <bits/stdc++.h>using namespace std;long long tour(long long n, vector<string> str,vector<vector<long long>> arr, long long q,vector<string> queries){map<string, long long> mp;for (long long i = 0; i < n; i++){mp[str[i]] = i;}long long i = 0, j = 0;long long ans = 0;for (long long k = 0; k < q; k++){string s = queries[k];j = mp[s];ans += arr[i][j];i = j;}return ans;}int main(){long long n = 3;vector<string> str = {"delhi","bengaluru","hyderabad"};vector<vector<long long>> arr = {{0, 10, 20},{10, 0, 55},{20, 55, 0}};long long q = 4;vector<string> queries = {"bengaluru","delhi","hyderabad","bengaluru"};cout << tour(n, str, arr, q, queries) << "\n";return 0;}
No comments:
Post a Comment