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.
Golang solution just in case someone is looking.
NOTE: Dijkstra will not work as explained [here]https://www.hackerrank.com/challenges/beautiful-path/forum/comments/121506). We need to go with brute force as input size is small, so a simple dfs/bfs needed to check all possible distances of a vertex from starting vertex(in this case 'A').
packagemainimport("bufio""fmt""io""os""strconv""strings""math")/* * Complete the 'beautifulPath' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. 2D_INTEGER_ARRAY edges * 2. INTEGER A * 3. INTEGER B */funcbeautifulPath(nint32,edges[][]int32,Aint32,Bint32)int32{constINF=int32(1024)adjList:=edgeListToAdjList(n,edges)varqueue[][]int32queue=append(queue,[]int32{A,0})dist:=initDist(n)dist[A][0]=trueresult:=INFforlen(queue)>0{front:=queue[0]u:=front[0]d:=front[1]queue=queue[1:]for_,edge:=rangeadjList[u]{v:=edge[0]w:=edge[1]newDistForV:=d|wif!dist[v][newDistForV]{dist[v][newDistForV]=truequeue=append(queue,[]int32{v,newDistForV})ifv==B{result=int32(math.Min(float64(result),float64(newDistForV)))}}}}ifresult==INF{return-1}returnresult}funcinitDist(Vint32)map[int32]map[int32]bool{dist:=make(map[int32]map[int32]bool)fori:=int32(0);i<=V;i++{dist[i]=make(map[int32]bool)}returndist}funcedgeListToAdjList(Vint32,edgeList[][]int32)[][][]int32{adjList:=make([][][]int32,V+1)for_,edge:=rangeedgeList{u:=edge[0]v:=edge[1]w:=edge[2]adjList[u]=append(adjList[u],[]int32{v,w})adjList[v]=append(adjList[v],[]int32{u,w})}returnadjList}funcmain(){reader:=bufio.NewReaderSize(os.Stdin,16*1024*1024)stdout,err:=os.Create(os.Getenv("OUTPUT_PATH"))checkError(err)deferstdout.Close()writer:=bufio.NewWriterSize(stdout,16*1024*1024)firstMultipleInput:=strings.Split(strings.TrimSpace(readLine(reader))," ")nTemp,err:=strconv.ParseInt(firstMultipleInput[0],10,64)checkError(err)n:=int32(nTemp)mTemp,err:=strconv.ParseInt(firstMultipleInput[1],10,64)checkError(err)m:=int32(mTemp)varedges[][]int32fori:=0;i<int(m);i++{edgesRowTemp:=strings.Split(strings.TrimRight(readLine(reader)," \t\r\n")," ")varedgesRow[]int32for_,edgesRowItem:=rangeedgesRowTemp{edgesItemTemp,err:=strconv.ParseInt(edgesRowItem,10,64)checkError(err)edgesItem:=int32(edgesItemTemp)edgesRow=append(edgesRow,edgesItem)}iflen(edgesRow)!=3{panic("Bad input")}edges=append(edges,edgesRow)}secondMultipleInput:=strings.Split(strings.TrimSpace(readLine(reader))," ")ATemp,err:=strconv.ParseInt(secondMultipleInput[0],10,64)checkError(err)A:=int32(ATemp)BTemp,err:=strconv.ParseInt(secondMultipleInput[1],10,64)checkError(err)B:=int32(BTemp)result:=beautifulPath(n,edges,A,B)fmt.Fprintf(writer,"%d\n",result)writer.Flush()}funcreadLine(reader*bufio.Reader)string{str,_,err:=reader.ReadLine()iferr==io.EOF{return""}returnstrings.TrimRight(string(str),"\r\n")}funccheckError(errerror){iferr!=nil{panic(err)}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Penalty Path
You are viewing a single comment's thread. Return to all comments →
Golang solution just in case someone is looking. NOTE: Dijkstra will not work as explained [here]https://www.hackerrank.com/challenges/beautiful-path/forum/comments/121506). We need to go with brute force as input size is small, so a simple dfs/bfs needed to check all possible distances of a vertex from starting vertex(in this case 'A').