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.
importjava.io.*;importjava.util.*;classPair{intfirst;intsecond;Pair(intf,ints){first=f;second=s;}Pair(){}}classPair3implementsComparable<Pair3>{intfirst;intsecond;intthird;Pair3(intf,ints,intt){first=f;second=s;third=t;}Pair3(){}@OverridepublicintcompareTo(Pair3tmp){if(first<tmp.first)return-1;elseif(first==tmp.first){if(second<tmp.second)return-1;elseif(second==tmp.second){if(third<tmp.third)return-1;elseif(third==tmp.third)return0;elsereturn1;}elsereturn1;}return1;}}publicclassHelloWorld{publicstaticvoiddijsktra(intn,intfishtypes[],List<List<Pair>>adj,List<List<Integer>>totalweight){Set<Pair3>set=newTreeSet<>();//add Pair3 = [distance, shop, types of fish] of shop 0 to setset.add(newPair3(0,0,fishtypes[0]));while(set.isEmpty()==false){Pair3node=set.iterator().next();set.remove(set.iterator().next());intcurdist=node.first;intcurshop=node.second;intcurTypesOfFish=node.third;if(totalweight.get(curshop).get(curTypesOfFish)<=curdist){//we will skip to the next node if the distance from 0 to curshop is smaller than this node's distance with the same types of fishcontinue;}totalweight.get(curshop).set(curTypesOfFish,curdist);for(inti=0;i<adj.get(curshop).size();i++){Pair3nextnode=newPair3();nextnode.first=curdist+adj.get(curshop).get(i).second;nextnode.second=adj.get(curshop).get(i).first;nextnode.third=curTypesOfFish|fishtypes[adj.get(curshop).get(i).first];//bit-maskingset.add(nextnode);}}}publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannersc=newScanner(System.in);intn=sc.nextInt();intm=sc.nextInt();intk=sc.nextInt();intfishtypes[]=newint[n];// bit-masking for types of fish sold by i-th shop for(inti=0;i<n;i++){intx=sc.nextInt();for(intj=0;j<x;j++){inttype=sc.nextInt();fishtypes[i]=fishtypes[i]|(1<<(type-1));}}//create adj listList<List<Pair>>adj=newArrayList<>();for(inti=0;i<n;i++){adj.add(newArrayList<>());}for(inti=0;i<m;i++){intshop1=sc.nextInt()-1;intshop2=sc.nextInt()-1;intweight=sc.nextInt();adj.get(shop1).add(newPair(shop2,weight));adj.get(shop2).add(newPair(shop1,weight));}//totalweight[i][j] save total weight from 1 to i-th shop with j types collectedList<List<Integer>>totalweight=newArrayList<>();for(inti=0;i<n;i++){totalweight.add(newArrayList<>());for(intj=0;j<((1<<k));j++){totalweight.get(i).add(Integer.MAX_VALUE);}}dijsktra(n,fishtypes,adj,totalweight);intres=Integer.MAX_VALUE;for(inti=0;i<totalweight.get(n-1).size();i++){if(totalweight.get(n-1).get(i)==Integer.MAX_VALUE)continue;for(intj=i;j<totalweight.get(n-1).size();j++){if(totalweight.get(n-1).get(j)==Integer.MAX_VALUE)continue;//check if 2 cats collect enough types of fishif((i|j)==(1<<k)-1){res=Math.min(res,Math.max(totalweight.get(n-1).get(i),totalweight.get(n-1).get(j)));}}}System.out.println(res);sc.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Synchronous Shopping
You are viewing a single comment's thread. Return to all comments →