'find out if vertices are connected to each other
I can't solve this problem, any help will be good.
teacher Açaizeiro Bórus Djorus is responsible for the new big project in his city: an aqueduct to supply açaí in almost every home. As the piping is expensive, he asked for your help to minimize costs and guarantee the energy of the people! His task is to find the lowest possible expense on the piping to connect all the homes as a supplier. Each meter of pipe costs R$ 3.14.
Input:
The first input line indicates how many there are n households in the region (1 ≤ n ≤ 100). The next n lines each represent information from a vertex in the format: id,d1,v1,d2,v2,⋯,dA,vA, where id is an integer identifying the residence, A is the number of neighbors of id, and each vi ≠ id is an integer identifying a neighboring house, which is located at a distance of di meters from id.
Exit:
Present the total value of the piping needed to connect all the homes.
Comments:
Each network user is confirmed by a unique id, the todograph is undirected, connected and loopless. In the first example, all vertices are connected to each other, and the minimum pipe required is 13 meters (R$ 40.82) connecting the houses in the following sequence: (3.1) and (1,2) In the second example, the minimum pipe required is 190 meters (R$ 596.60) connecting the residences in the following sequence: (3.1), (1,5),(5,4),(4,2) and (2, 6).
this is my code so far:
vertex, edges = [], []
for _ in range(int(input())):
v, A, *neighbors = input().split()
vertex.append(int(v))
for i in range(0, len(neighbors), 2):
edges.append((int(neighbors[i]), vertex[-1], int(neighbors[i + 1])))
total = 0
edges.sort(reverse=True)
agm = {v: set() for v in vertex}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

