Tutte le sfide
Difficile 1 risolta 100.0% di accettazione

Minimum Network Connection Cost

Choose undirected links so that every vertex can reach every other vertex, with minimum total cost.

Cosa imparerai

  • Connect a graph cheaply with Kruskal's algorithm
  • Detect cycles with a disjoint-set union
  • Report when a graph cannot be connected

Problema

Mostra traduzione

Choose undirected links so that every vertex can reach every other vertex, with minimum total cost. A link can be used in either direction. Return −1 if the available links cannot connect the whole network. You do not need to return the selected links.

Implement minimum_network_connection_cost(vertices, links).

Input

vertices: The number of network vertices.

links: Available links [u, v, cost].

Output

Return the minimum total cost, −1 if impossible, or 0 for a one-vertex network.

Vincoli

1 ≤ vertices ≤ 100,000. 0…200,000 links; endpoints are distinct vertices in 1…vertices. Costs are 1…1,000,000,000. Parallel links are allowed. The total fits signed 64-bit.

Esempi

Esempio 1
Input:
vertices = 3
links = [
    [1, 2, 4],
    [2, 3, 2],
    [1, 3, 7]
]

Output atteso:
expected = 6
Input:
long long vertices = 3;
std::vector<std::vector<long long>> links = {
    {1, 2, 4},
    {2, 3, 2},
    {1, 3, 7}
};

Output atteso:
long long expected = 6;
Input:
long vertices = 3;
long[][] links = new long[][] {
    new long[] {1, 2, 4},
    new long[] {2, 3, 2},
    new long[] {1, 3, 7}
};

Output atteso:
long expected = 6;

Spiegazione

Choose links costing 4 and 2 to connect all three vertices. Their total is 6; the cost-7 link is unnecessary.

Esempio 2
Input:
vertices = 3
links = [
    [1, 2, 4]
]

Output atteso:
expected = -1
Input:
long long vertices = 3;
std::vector<std::vector<long long>> links = {
    {1, 2, 4}
};

Output atteso:
long long expected = -1;
Input:
long vertices = 3;
long[][] links = new long[][] {
    new long[] {1, 2, 4}
};

Output atteso:
long expected = -1;

Spiegazione

Vertex 3 has no connection. No selection can connect the entire network.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

You need a cheapest set of links that still keeps every vertex connected, which suggests a spanning tree.

Suggerimento 2

Greedily add the cheapest link that does not create a cycle, or grow components with a union-find structure.

Suggerimento 3

If fewer than vertices minus one usable links connect the graph, the answer is impossible.

Complessità attesa

O(links log links) time, O(vertices + links) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 Н"Никита "Programmer" Python 848 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.