Tất cả thử thách
Khó 1 người đã giải Tỷ lệ chấp nhận 100.0%

Minimum Network Connection Cost

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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.

Ví dụ

Ví dụ 1
Đầu vào:
vertices = 3
links = [
    [1, 2, 4],
    [2, 3, 2],
    [1, 3, 7]
]

Kết quả mong đợi:
expected = 6
Đầu vào:
long long vertices = 3;
std::vector<std::vector<long long>> links = {
    {1, 2, 4},
    {2, 3, 2},
    {1, 3, 7}
};

Kết quả mong đợi:
long long expected = 6;
Đầu vào:
long vertices = 3;
long[][] links = new long[][] {
    new long[] {1, 2, 4},
    new long[] {2, 3, 2},
    new long[] {1, 3, 7}
};

Kết quả mong đợi:
long expected = 6;

Giải thích

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

Ví dụ 2
Đầu vào:
vertices = 3
links = [
    [1, 2, 4]
]

Kết quả mong đợi:
expected = -1
Đầu vào:
long long vertices = 3;
std::vector<std::vector<long long>> links = {
    {1, 2, 4}
};

Kết quả mong đợi:
long long expected = -1;
Đầu vào:
long vertices = 3;
long[][] links = new long[][] {
    new long[] {1, 2, 4}
};

Kết quả mong đợi:
long expected = -1;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 848 ms - 5/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.