<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0590_-_Prim</id>
	<title>0590 - Prim - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0590_-_Prim"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0590_-_Prim&amp;action=history"/>
	<updated>2026-05-01T05:38:08Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=0590_-_Prim&amp;diff=9826&amp;oldid=prev</id>
		<title>Oros Ioana Diana: Pagină nouă: = Cerința = Se dă un graf neorientat ponderat conex cu &lt;code&gt;n&lt;/code&gt; vârfuri și &lt;code&gt;m&lt;/code&gt; muchii – în care fiecare muchie are asociat un cost, număr natural strict pozitiv. Folosind algoritmul lui Prim, determinați un arbore parțial de cost minim, cu rădăcina în vârful &lt;code&gt;1&lt;/code&gt;.  = Date de intrare = Fișierul de intrare &lt;code&gt;primIN.txt&lt;/code&gt; conține pe prima linie numerele &lt;code&gt;n m&lt;/code&gt;, iar următoarele linii câte un triplet &lt;code&gt;i j c&lt;/code...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0590_-_Prim&amp;diff=9826&amp;oldid=prev"/>
		<updated>2024-05-18T19:50:44Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerința = Se dă un graf neorientat ponderat conex cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii – în care fiecare muchie are asociat un cost, număr natural strict pozitiv. Folosind algoritmul lui Prim, determinați un arbore parțial de cost minim, cu rădăcina în vârful &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.  = Date de intrare = Fișierul de intrare &amp;lt;code&amp;gt;primIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numerele &amp;lt;code&amp;gt;n m&amp;lt;/code&amp;gt;, iar următoarele linii câte un triplet &amp;lt;code&amp;gt;i j c&amp;lt;/code...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Cerința =&lt;br /&gt;
Se dă un graf neorientat ponderat conex cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii – în care fiecare muchie are asociat un cost, număr natural strict pozitiv. Folosind algoritmul lui Prim, determinați un arbore parțial de cost minim, cu rădăcina în vârful &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;primIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numerele &amp;lt;code&amp;gt;n m&amp;lt;/code&amp;gt;, iar următoarele linii câte un triplet &amp;lt;code&amp;gt;i j c&amp;lt;/code&amp;gt;, cu semnificația: există muchia &amp;lt;code&amp;gt;(i j)&amp;lt;/code&amp;gt; și are costul &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;primOUT.txt&amp;lt;/code&amp;gt; va conține pe prima linie costul arborelui de cost minim determinat, iar pe a doua linie vectorul de tați al arborelui, cu elementele separate prin exact un spațiu. În cazul în care restricțiile nu sunt îndeplinite, se va afișa mesajul &amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ n ≤ 100&amp;lt;/code&amp;gt;&lt;br /&gt;
* costul unei muchii va fi mai mic decât &amp;lt;code&amp;gt;1000&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;primIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 7 11&lt;br /&gt;
 1 2 2&lt;br /&gt;
 1 7 4&lt;br /&gt;
 2 3 3&lt;br /&gt;
 2 5 2&lt;br /&gt;
 2 6 3&lt;br /&gt;
 2 7 3&lt;br /&gt;
 3 4 1&lt;br /&gt;
 3 5 2&lt;br /&gt;
 4 5 1&lt;br /&gt;
 5 6 3&lt;br /&gt;
 6 7 5&lt;br /&gt;
&amp;lt;code&amp;gt;primOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 12&lt;br /&gt;
 0 1 4 5 2 2 2 &lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;primIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 0 11&lt;br /&gt;
 1 2 2&lt;br /&gt;
 1 7 4&lt;br /&gt;
 2 3 3&lt;br /&gt;
 2 5 2&lt;br /&gt;
 2 6 3&lt;br /&gt;
 2 7 3&lt;br /&gt;
 3 4 1&lt;br /&gt;
 3 5 2&lt;br /&gt;
 4 5 1&lt;br /&gt;
 5 6 3&lt;br /&gt;
 6 7 5&lt;br /&gt;
&amp;lt;code&amp;gt;primOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 Datele nu corespund restrictiilor impuse&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
import heapq&lt;br /&gt;
&lt;br /&gt;
def check_restrictions(n, edges):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 100):&lt;br /&gt;
        return False&lt;br /&gt;
    for x, y, c in edges:&lt;br /&gt;
        if c &amp;gt;= 1000:&lt;br /&gt;
            return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;primIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as infile:&lt;br /&gt;
        n, m = map(int, infile.readline().strip().split())&lt;br /&gt;
        edges = []&lt;br /&gt;
        for _ in range(m):&lt;br /&gt;
            x, y, c = map(int, infile.readline().strip().split())&lt;br /&gt;
            edges.append((x, y, c))&lt;br /&gt;
&lt;br /&gt;
    if not check_restrictions(n, edges):&lt;br /&gt;
        with open(&amp;quot;primOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as outfile:&lt;br /&gt;
            outfile.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    G = [[] for _ in range(n + 1)]&lt;br /&gt;
    for x, y, c in edges:&lt;br /&gt;
        G[x].append((c, y))&lt;br /&gt;
        G[y].append((c, x))&lt;br /&gt;
&lt;br /&gt;
    V = [False] * (n + 1)&lt;br /&gt;
    T = [-1] * (n + 1)&lt;br /&gt;
    D = [float(&amp;#039;inf&amp;#039;)] * (n + 1)&lt;br /&gt;
&lt;br /&gt;
    Q = []&lt;br /&gt;
    V[1] = True&lt;br /&gt;
    T[1] = 0&lt;br /&gt;
    D[1] = 0&lt;br /&gt;
&lt;br /&gt;
    for c, y in G[1]:&lt;br /&gt;
        T[y] = 1&lt;br /&gt;
        D[y] = c&lt;br /&gt;
        heapq.heappush(Q, (c, y))&lt;br /&gt;
&lt;br /&gt;
    S = 0&lt;br /&gt;
    for k in range(1, n):&lt;br /&gt;
        while V[Q[0][1]]:&lt;br /&gt;
            heapq.heappop(Q)&lt;br /&gt;
        c, u = heapq.heappop(Q)&lt;br /&gt;
        V[u] = True&lt;br /&gt;
        S += c&lt;br /&gt;
        for cost, v in G[u]:&lt;br /&gt;
            if not V[v] and D[v] &amp;gt; cost:&lt;br /&gt;
                T[v] = u&lt;br /&gt;
                D[v] = cost&lt;br /&gt;
                heapq.heappush(Q, (cost, v))&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;primOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as outfile:&lt;br /&gt;
        outfile.write(f&amp;quot;{S}\n&amp;quot;)&lt;br /&gt;
        outfile.write(&amp;quot; &amp;quot;.join(map(str, T[1:])))&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Oros Ioana Diana</name></author>
	</entry>
</feed>