• + 0 comments

    Solution in Python

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    import heapq as pq
    from collections import defaultdict
    from collections import deque
    
    #
    # Complete the 'shop' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts following parameters:
    #  1. INTEGER n
    #  2. INTEGER k
    #  3. STRING_ARRAY centers
    #  4. 2D_INTEGER_ARRAY roads
    #
    
    def getFishset(k, centers, bitset):
        for centerN in range(0, len(centers)):
            centers[centerN] = centers[centerN].rstrip().split()
            limit = int(centers[centerN][0])
            for each_f in range(1, limit + 1):
                bitset[centerN + 1] |= 1 << (int(centers[centerN][each_f]) - 1)
        return bitset
    
    def getPath(n, k, centers, roads, bitset, graph):
        hQ = []
        power2 = 1 << k
        dist = [[999999 for j in range(power2)] for _ in range(n + 1)]
        visited = [[0 for j in range(power2)] for _ in range(n + 1)]
        
        bitset = getFishset(k, centers, bitset)
        pq.heappush(hQ, (0, 1, bitset[1]))
        dist[1][bitset[1]] = 0
    
        while hQ:
            distance, centerN, eachSet = pq.heappop(hQ)
            
            if visited[centerN][eachSet] == 1:
                continue
            visited[centerN][eachSet] = 1
            
            for neighbor in graph[centerN]:
                bitadd = eachSet | bitset[neighbor[0]]
                f_dist = distance + neighbor[1]
                if f_dist < dist[neighbor[0]][bitadd]:
                    dist[neighbor[0]][bitadd] = f_dist
                    pq.heappush(hQ, (f_dist, neighbor[0], bitadd))
        
        totalCost = 999999
        range_l = len(dist[n])
        
        for i in range(range_l - 1, 0, -1):
            if dist[n][i] == 999999:
                continue
            if i == (power2 - 1) and dist[n][i] < totalCost:
                totalCost = dist[n][i]
                continue
            for j in range(1, i):
                if (i | j) == (power2 - 1) and max(dist[n][i], dist[n][j]) < totalCost:
                    totalCost = max(dist[n][i], dist[n][j])
    
        return totalCost
    
    def shop(n, k, centers, roads):
        bitset = [0 for _ in range(n + 1)]
        graph = defaultdict(list)
        
        for each in roads:
            graph[each[0]].append((each[1], each[2]))
            graph[each[1]].append((each[0], each[2]))
    
        totalCost = getPath(n, k, centers, roads, bitset, graph)
        return totalCost
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
        n = int(first_multiple_input[0])
        m = int(first_multiple_input[1])
        k = int(first_multiple_input[2])
    
        centers = []
        for _ in range(n):
            centers_item = input()
            centers.append(centers_item)
    
        roads = []
        for _ in range(m):
            roads.append(list(map(int, input().rstrip().split())))
    
        res = shop(n, k, centers, roads)
    
        fptr.write(str(res) + '\n')
        fptr.close()