classSolution{public:intreachableNodes(vector<vector<int>>&edges,intmaxMoves,intn){usingP=pair<int,int>;vector<vector<P>>graph(n);priority_queue<P,vector<P>,greater<>>minHeap;// (d, u)vector<int>dist(n,maxMoves+1);for(constvector<int>&e:edges){constintu=e[0];constintv=e[1];constintcnt=e[2];graph[u].emplace_back(v,cnt);graph[v].emplace_back(u,cnt);}minHeap.emplace(0,0);dist[0]=0;while(!minHeap.empty()){constauto[d,u]=minHeap.top();minHeap.pop();// Already takes maxMoves to reach u, can't explore anymoreif(dist[u]>=maxMoves)break;for(constauto&[v,w]:graph[u]){constintnewDist=d+w+1;if(newDist<dist[v]){dist[v]=newDist;minHeap.emplace(newDist,v);}}}constintreachableNodes=count_if(begin(dist),end(dist),[&](intd){returnd<=maxMoves;});intreachableSubnodes=0;for(constvector<int>&e:edges){constintu=e[0];constintv=e[1];constintcnt=e[2];// Reachable nodes of e from uconstinta=dist[u]>maxMoves?0:min(maxMoves-dist[u],cnt);// Reachable nodes of e from vconstintb=dist[v]>maxMoves?0:min(maxMoves-dist[v],cnt);reachableSubnodes+=min(a+b,cnt);}returnreachableNodes+reachableSubnodes;}};
classSolution{publicintreachableNodes(int[][]edges,intmaxMoves,intn){List<Pair<Integer,Integer>>[]graph=newList[n];Queue<int[]>minHeap=newPriorityQueue<>((a,b)->a[0]-b[0]);// (d, u)int[]dist=newint[n];Arrays.fill(dist,maxMoves+1);for(inti=0;i<n;++i)graph[i]=newArrayList<>();for(int[]e:edges){finalintu=e[0];finalintv=e[1];finalintcnt=e[2];graph[u].add(newPair<>(v,cnt));graph[v].add(newPair<>(u,cnt));}minHeap.offer(newint[]{0,0});dist[0]=0;while(!minHeap.isEmpty()){finalintd=minHeap.peek()[0];finalintu=minHeap.poll()[1];// Already takes maxMoves to reach u, can't explore anymoreif(d>=maxMoves)break;for(Pair<Integer,Integer>node:graph[u]){finalintv=node.getKey();finalintw=node.getValue();finalintnewDist=d+w+1;if(newDist<dist[v]){dist[v]=newDist;minHeap.offer(newint[]{newDist,v});}}}finalintreachableNodes=(int)Arrays.stream(dist).filter(d->d<=maxMoves).count();intreachableSubnodes=0;for(int[]e:edges){finalintu=e[0];finalintv=e[1];finalintcnt=e[2];// Reachable nodes of e from ufinalinta=dist[u]>maxMoves?0:Math.min(maxMoves-dist[u],cnt);// Reachable nodes of e from vfinalintb=dist[v]>maxMoves?0:Math.min(maxMoves-dist[v],cnt);reachableSubnodes+=Math.min(a+b,cnt);}returnreachableNodes+reachableSubnodes;}}
classSolution:defreachableNodes(self,edges:List[List[int]],maxMoves:int,n:int)->int:graph=[[]for_inrange(n)]minHeap=[(0,0)]# (d, u)dist=[maxMoves+1]*ndist[0]=0foru,v,cntinedges:graph[u].append((v,cnt))graph[v].append((u,cnt))whileminHeap:d,u=heapq.heappop(minHeap)# Already takes maxMoves to reach u, can't explore anymoreifdist[u]>=maxMoves:breakforv,wingraph[u]:newDist=d+w+1ifnewDist<dist[v]:dist[v]=newDistheapq.heappush(minHeap,(newDist,v))reachableNodes=sum(d<=maxMovesfordindist)reachableSubnodes=0foru,v,cntinedges:# Reachable nodes of e from ua=0ifdist[u]>maxMoveselsemin(maxMoves-dist[u],cnt)# Reachable nodes of e from vb=0ifdist[v]>maxMoveselsemin(maxMoves-dist[v],cnt)reachableSubnodes+=min(a+b,cnt)returnreachableNodes+reachableSubnodes