Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
0484 - Lant Minim
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Exemplul 2 = <code>lantminimIN.txt</code> 101 101 101 <code>lantminimOUT.txt</code> Datele nu corespund restrictiilor impuse == Rezolvare == <syntaxhighlight lang="python3" line="1"> import heapq def dijkstra(graf, start, end): # Inițializare distanțe și coadă de priorități distante = {varf: float('infinity') for varf in graf} distante[start] = 0 coada_prioritati = [(0, start)] while coada_prioritati: distanta_curenta, varf_curent = heapq.heappop(coada_prioritati) # Verificare dacă am găsit un drum mai scurt if distanta_curenta > distante[varf_curent]: continue for vecin, pondere in graf[varf_curent]: distanta = distanta_curenta + pondere # Actualizare distanțe și adăugare în coadă if distanta < distante[vecin]: distante[vecin] = distanta heapq.heappush(coada_prioritati, (distanta, vecin)) # Construire lanț minim drum_minim = [] varf_curent = end while varf_curent != start: drum_minim.insert(0, varf_curent) varf_curent = graf[varf_curent][0][0] # Se folosește primul vecin în loc de [1] drum_minim.insert(0, start) return drum_minim def verifica_restrictii(n, muchii, p, q): # Verificare restricții if not (1 <= n <= 100 and all(1 <= i <= n and 1 <= j <= n for i, j in muchii)): return False # Alte verificări specifice, dacă este cazul return True # Citire date din fișierul de intrare with open("lantminimIN.txt", "r") as fisier: n, p, q = map(int, fisier.readline().split()) muchii = [tuple(map(int, linie.split())) for linie in fisier.readlines()] # Verificare restricții if not verifica_restrictii(n, muchii, p, q): with open("lantminimOUT.txt", "w") as fisier: fisier.write("Datele nu corespund restrictiilor impuse") else: # Construire graf graf = {i: [] for i in range(1, n + 1)} for muchie in muchii: graf[muchie[0]].append((muchie[1], 1)) graf[muchie[1]].append((muchie[0], 1)) # Aplicare algoritm Dijkstra drum_minim = dijkstra(graf, p, q) # Scriere rezultat în fișierul de ieșire with open("lantminimOUT.txt", "w") as fisier: fisier.write(str(len(drum_minim)) + "\n") fisier.write(" ".join(map(str, drum_minim))) </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width