Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Metro problem solution

YASH PAL, 31 July 2024
In this HackerEarth Metro problem solution A city subway line has become huge and it is hard to take the shortest path through them. You have to find the shortest path in subway lines. In the second 0, you are in the station start and you want to go to the station end.
The city has  stations. The subway has m lines. Each subway line goes to some stations.
The i – th subway goes to stations u(i,1),u(i,2),u(i,3),…,u(ii,ki) (in order) and this train takes w(i,j) seconds to travel from u(i,j) to u(i,j+1)(for 1 <=i <= m and 1 <= j <= ki).
Trains are ready for the passengers to get in, but the last train goes in the second ti(and you are allowed to board it in between the path).
HackerEarth Metro problem solution

HackerEarth Metro problem solution.

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, pii> pi_ii;
const int maxn = 1e5;
const int inf = 1e18;

int n, m, st, en;
//edge: u v last w
vector<pi_ii> vc[maxn];

void init(){
int M = 0;
cin >> n >> m;
for(int _ = 0; _ < m; _++){
int t, last;
cin >> t >> last;
int v[t];
int w[t-1];

for(int j = 0; j < t; j++)
cin >> v[j], v[j]--;

for(int j = 0; j + 1 < t; j++)
cin >> w[j];
int cur_last = last;
for(int j = 0; j + 1 < t; j++){
vc[v[j]].push_back(pi_ii(v[j + 1], pii(w[j], cur_last)));
cur_last += w[j];
}
M += t;
}
cin >> st >> en;
st--;
en--;
m = M;
}

int dis[maxn];
void DIJKSTRA(int start){
fill(dis, dis + n, inf);
dis[start] = 0;
set<pii> st;
for(int i = 0; i < n; i++)
st.insert(pii(dis[i], i));

while(st.size()){
int u = st.begin()->second;
st.erase(st.begin());
for(pi_ii edge: vc[u]){
int v = edge.first;
int w = edge.second.first;
int last = edge.second.second;
if(dis[u] <= last){
int cur = dis[u] + w;
if(cur < dis[v]) {
st.erase(pii(dis[v], v));
dis[v] = cur;
st.insert(pii(dis[v], v));
}
}
}
}
}


main(){
init();
DIJKSTRA(st);
if(dis[en] == inf)
cout << -1 << endl;
else
cout << dis[en] << endl;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 1e5 + 14;
int n, m, st, t;
ll d[maxn];
struct Edge{
int u, w;
ll t;
};
vector<Edge> g[maxn];
int ver[maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
while(m--){
int sz;
ll t;
cin >> sz >> t;
for(int i = 0; i < sz; i++)
cin >> ver[i], ver[i]--;
for(int i = 0; i < sz - 1; i++){
int w;
cin >> w;
g[ ver[i] ].push_back({ver[i + 1], w, t});
t += w;
}
}
cin >> st >> t;
st--, t--;
memset(d, 63, sizeof d);
d[st] = 0;
set<pair<ll, int> > s({{d[st], st}});
while(s.size()){
int v = s.begin() -> second;
if(v == t)
return cout << d[v] << 'n', 0;
s.erase(s.begin());
for(auto edge : g[v])
if(d[v] <= edge.t && d[edge.u] > d[v] + edge.w){
s.erase({d[edge.u], edge.u});
d[edge.u] = d[v] + edge.w;
s.insert({d[edge.u], edge.u});
}
}
cout << "-1n";
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes