affiche juste le code, pas d'explications !
- User
System: affiche juste le code, pas d'explications ! User: Tu es un développeur python3 expérimenté, et tu dois concevoir un programme en python (avec des commentaires en français, sans utiliser de librairies externes, sans
if __name__=="__main__":, avec type hints, et en privilégiant les algorythmes a complexité constante (O(1)) et/ou complexité linéaire (O(n))). Le programme va recevoir les données en entrée sur la stdin, et doit répondre sur la stdout. Fabrique moi un programme python qui résout le problème (énoncé en markdown) suivant :Water Distribution Optimization
Your city's water system is in need of repair, but given the limited resources at your disposal, it is important to identify the most leaking part of the system in order to treat it as quickly as possible.
The water system is represented by a binary tree of pipes. (https://fr.wikipedia.org/wiki/Arbre_enraciné)
The root of the tree is the water distribution point (the source), the other vertices of the tree are all the points that need to be supplied with water.
If a quantity of water has to be transported to a point of the network, it will necessarily pass through all the pipes of the unique path between the source and this point.
Each pipe connecting 2 vertices has a hydraulic loss coefficient. The total loss of a pipe is equal to the total quantity of water passing through this pipe multiplied by the loss coefficient of this pipe.
Data
Input
Line 1: an integer N representing the number of points requiring water supply. Point 0 is the source, the other points are numbered from 1 to N (inclusive).
N is between 2 and 200.
Line 2 to N+2 : Two possibilities
- Let 6 integers n, q, f1, p1, f2, p2 representing the number of the point, the quantity of water to be conveyed to this point, the pipe connecting point n to point f1 with a loss coefficient of p1 and the pipe connecting n to f2 with a loss coefficient of p2.
- Let two integers n and q represent the number of the point followed by the quantity of water to be conveyed to this point.
Note: all the lines with 6 integers (which represent internal vertices of the binary tree) are always present before the lines with 2 integers (which represent leaves of the tree). As it is a strict binary tree, we can then note that N is always even, and that we always have N/2 lines with 6 integers and N/2+1 lines with 2 integers.
The quantities of water and the loss coefficients are between 1 and 10.000.
Output
An integer representing the maximum loss of a pipe on the network.
Example
This input represents the image below.
- Assistant
'''python import sys from collections import defaultdict
Définition de la fonction pour calculer la perte d'eau maximale
def calculer_perte_maximale() -> int: # Lecture du nombre de points N: int = int(sys.stdin.readline().strip())
# Dictionnaire pour stocker les pertes d'eau par pipe pertes: defaultdict[int, int] = defaultdict(int) # Dictionnaire pour stocker la demande en eau de chaque point demandes: dict[int, int] = {} # Lecture des données des points for _ in range(N // 2): # Lecture des 6 entiers pour les points internes n, q, f1, p1, f2, p2 = map(int, sys.stdin.readline().strip().split()) # Stocke la demande d'eau pour le point demandes[n] = q # Ajout des pipes vers f1 et f2 avec leur perte pertes[(n, f1)] = p1 pertes[(n, f2)] = p2 # Pour les feuilles for _ in range(N // 2 + 1): # Lecture des 2 entiers pour les feuilles n, q = map(int, sys.stdin.readline().strip().split()) # Stocke la demande d'eau pour le point demandes[n] = q # Dictionnaire pour cumuler les demandes d'eau de chaque sub-tree cumul_demande: defaultdict[int, int] = defaultdict(int) # Fonction pour calculer la demande cumulée d'eau pour chaque sous-arbre def calculer_demande_cumulee(node: int) -> int: cumulative_water = demandes.get(node, 0) for child, _ in [key for key in pertes.keys() if key[0] == node]: cumulative_water += calculer_demande_cumulee(child) cumul_demande[node] = cumulative_water return cumulative_water # Commence le calcul de la demande cumulée depuis la source (0) calculer_demande_cumulee(0) # Calculer la perte maximale perte_maximale: int = 0 for (parent, child), loss_coefficient in pertes.items(): perte = cumul_demande[child] * loss_coefficient perte_maximale = max(perte_maximale, perte) return perte_maximale