A trickier Dijkstra.
#include <cmath>#include<cstdio>#include<vector>#include<string>#include<iostream>#include<algorithm>#include<queue>#include<unordered_map>#include<unordered_set>using namespacestd;Const intINF = std::numeric_limits<int>:: Max (); typedef pair<int,int> Node;//index-diststructcomp{int operator() (ConstNode &v1,ConstNode &v2) { returnV1.second >V2.second; }};intMain () {intT CIN >>T; while(t--) { //Setup Graphunordered_map<int, unordered_set<int>>G; for(inti =1; I < -; i++) for(intD =1; D <=6; d++) if((i + D) <= -) G[i].insert (i +d); intN CIN >> N;//Ladder while(n--) { intA, B; Cin >> a >>b; G[a].clear (); G[a].insert (b); } intM Cin >> m;//Snake while(m--) { intA, B; Cin >> a >>b; G[a].clear (); G[a].insert (b); } //Dijstravector<int> Dist (101, INF); Vector<int> God (101,0); dist[1] =0; Priority_queue<node, Vector<node>, comp>HP; Hp.push (Node (1,0)); while(!Hp.empty ()) {Node I=hp.top (); Hp.pop (); for(Auto &C:g[i.first]) { if(Dist[c] = = INF | | (Dist[i.first] +1) <Dist[c]) {Dist[c]= Dist[i.first] +1; GOD[C]=God[i.first]; if(C > I.first && (C-i.first) >6) || (C <I.first)) {God[c]+=1; } if(c = = -) { while(!hp.empty ()) Hp.pop (); Break; } Hp.push (Node (c, dist[c])); } } } intRET = dist[ -]; cout<< (ret = = INF?-1: ret-god[ -]) <<Endl; } return 0;}
View Code
Hackerrank-"Snakes and ladders:the quickest-up"