Yn 'e Haven Logies

Tiny house It Skûtsje

**Solution Explanation** For every test case we are given * `N` – number of houses * `M` – number of roads * `K` – number of houses that are already “good” * `C` – cost of building a new house on an empty plot * `D` – cost of repairing a damaged house The `K` good houses are numbered `1 … K`. The remaining `N – K` houses are damaged and have to be repaired at cost `D` each. The `M` roads are undirected and connect two houses. If a road connects a good house with a damaged house, the damaged house can be repaired for free – the road “carries” the repair. If a road connects two damaged houses, we can build a new house on one of the two endpoints (cost `C`) and then repair the other damaged house for free. The task is to find the minimum total cost to make **all** houses good. -------------------------------------------------------------------- #### 1. Observations * A damaged house that is connected to a good house can be repaired for free. So we only have to consider damaged houses that are **not** connected to any good house. * Consider a connected component of the graph that contains **no** good house. All houses inside this component are damaged. We can repair all of them by building a new house on **one** vertex of the component (cost `C`) and then repairing the remaining `size‑1` houses for free. Therefore the cost for such a component is `C`. * If a component contains at least one good house, all its damaged houses can be repaired for free – no extra cost is needed. * The graph is undirected, so the components are the usual connected components. -------------------------------------------------------------------- #### 2. Algorithm For each test case 1. Build the adjacency list of the graph. 2. Run a DFS/BFS to find all connected components. While exploring a component keep two flags: * `hasGood` – whether the component contains a good house. * `size` – number of vertices in the component. 3. After finishing a component * if `hasGood` is **false** → add `C` to the answer. * otherwise → add `0` (no cost). 4. Output the accumulated answer. The algorithm visits every vertex and edge once, so its complexity is ``` Time : O(N + M) per test case Memory : O(N + M) ``` which easily satisfies the limits. -------------------------------------------------------------------- #### 3. Correctness Proof We prove that the algorithm outputs the minimum possible cost. --- ##### Lemma 1 If a damaged house is in a component that contains a good house, then it can be repaired for free. **Proof.** Because the component contains a good house, there exists a path from that good house to the damaged house. All edges on that path are roads, so the damaged house is connected to a good house by a road. By the problem statement, a damaged house connected to a good house can be repaired for free. ∎ ##### Lemma 2 If a component contains no good house, then at least one new house must be built inside that component. **Proof.** All houses in the component are damaged. Without building a new house, the only way to repair a damaged house is to connect it to a good house by a road. But the component has no good house, and all roads stay inside the component, so no damaged house can be repaired for free. Therefore at least one new house must be built. ∎ ##### Lemma 3 If a component contains no good house, building a new house on any single vertex of the component and repairing the remaining vertices for free achieves the minimum possible cost for that component. **Proof.** By Lemma 2 at least one new house is necessary, costing `C`. After building it, every other vertex in the component is connected to this new good house by a path of roads, so by Lemma 1 all remaining damaged houses can be repaired for free. Thus the total cost for the component is exactly `C`, and no smaller cost is possible. ∎ ##### Lemma 4 For a component that contains a good house, the minimum cost is `0`. **Proof.** By Lemma 1 every damaged house in the component can be repaired for free, so no money has to be spent. Obviously a cost of `0` is attainable and no negative cost exists. ∎ ##### Theorem For each test case the algorithm outputs the minimum total cost to make all houses good. **Proof.** The algorithm processes each connected component independently. * If the component contains a good house, by Lemma 4 the optimal cost is `0`, and the algorithm adds `0`. * If the component contains no good house, by Lemma 3 the optimal cost is `C`, and the algorithm adds `C`. Summing over all components gives the optimal total cost for the whole graph. Therefore the algorithm is optimal. ∎ -------------------------------------------------------------------- #### 4. Reference Implementation (Python 3) ```python import sys sys.setrecursionlimit(1 << 25) def solve() -> None: data = list(map(int, sys.stdin.buffer.read().split())) it = iter(data) t = next(it) out_lines = [] for _ in range(t): N = next(it) M = next(it) K = next(it) C = next(it) D = next(it) # D is not needed in the algorithm # adjacency list adj = [[] for _ in range(N + 1)] for _ in range(M): u = next(it) v = next(it) adj[u].append(v) adj[v].append(u) visited = [False] * (N + 1) def dfs(u: int, has_good: list, size: list) -> None: """DFS that updates has_good[0] and size[0].""" visited[u] = True size[0] += 1 if u <= K: # good house has_good[0] = True for v in adj[u]: if not visited[v]: dfs(v, has_good, size) total_cost = 0 for node in range(1, N + 1): if not visited[node]: has_good = [False] size = [0] dfs(node, has_good, size) if not has_good[0]: total_cost += C out_lines.append(str(total_cost)) sys.stdout.write("\n".join(out_lines)) if __name__ == "__main__": solve() ``` The program follows exactly the algorithm proven correct above and conforms to the required input/output format.


Disponibilité et réservation immédiate Des photos (21)
Nos forfaits disponibles :
  • 2 Appartement It Skûtsje arrangement.

En option pour r&#233;server : Vélo électrique

15.00 - 22.00
10.30
Max. 2 Personnes
30 Mètre carré.

• Barbecue • Savon • Banque • Garde-robe • Réfrigérateur • Bouilloire électrique • Douche • Les serviettes • Chauffage • cuisine • Shampooing • Terrasse • Smart TV • Télévision par câble • Draps de lit • Vue sur le lac • Lave-vaisselle • Sèche-cheveux • Articles de toilette • Salle à manger • Ustensiles de cuisine • papier toilette • Vue du jardin • Lit king size • Entrée indépendante • Lampe de lecture • Verres à vin • Salle de bains • Sauna infrarouge • Torchons de cuisine • Douche à l'italienne • Accès avec clé normale • Jardin clos • Meubles de patio • Climatisation • Prise électrique à côté du lit • Extincteur d'incendie • Machine Nespressot • services de diffusion en continu

Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje
Image de Tiny house It Skûtsje