<?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=0126_-_D_Max</id>
	<title>0126 - D Max - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0126_-_D_Max"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0126_-_D_Max&amp;action=history"/>
	<updated>2026-05-02T03:13:30Z</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=0126_-_D_Max&amp;diff=8008&amp;oldid=prev</id>
		<title>Simina: Pagină nouă: == Enunț == Se considerã un graf neorientat conex cu &lt;code&gt;n&lt;/code&gt; vârfuri, numerotate de la &lt;code&gt;1&lt;/code&gt; la &lt;code&gt;n&lt;/code&gt;, şi &lt;code&gt;m&lt;/code&gt; muchii. Definim distanţa minimă între două noduri &lt;code&gt;x&lt;/code&gt; şi &lt;code&gt;y&lt;/code&gt; ca fiind numărul minim de muchii al unui lanţ elementar care uneşte &lt;code&gt;x&lt;/code&gt; cu &lt;code&gt;y&lt;/code&gt;.  = Cerinţa = Sã se determine nodul aflat la cea mai mare distanţã minimă faţã de nodul &lt;code&gt;1&lt;/code&gt;.  = Date de intrare = Fişie...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0126_-_D_Max&amp;diff=8008&amp;oldid=prev"/>
		<updated>2023-12-13T15:58:04Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: == Enunț == Se considerã un graf neorientat conex cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri, numerotate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, şi &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii. Definim distanţa minimă între două noduri &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; ca fiind numărul minim de muchii al unui lanţ elementar care uneşte &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;.  = Cerinţa = Sã se determine nodul aflat la cea mai mare distanţã minimă faţã de nodul &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.  = Date de intrare = Fişie...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Enunț ==&lt;br /&gt;
Se considerã un graf neorientat conex cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri, numerotate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, şi &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii. Definim distanţa minimă între două noduri &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; ca fiind numărul minim de muchii al unui lanţ elementar care uneşte &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Cerinţa =&lt;br /&gt;
Sã se determine nodul aflat la cea mai mare distanţã minimă faţã de nodul &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;dmaxIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie două numere &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, reprezentând numărul de noduri, respectiv numărul de muchii. Fiecare dintre urmãtoarele &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; linii va conţine câte două numere &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;, separate printr-un spaţiu, cu semnificaţia: existã o muchie între nodul &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi nodul &amp;lt;code&amp;gt;y&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;dmaxOUT.txt&amp;lt;/code&amp;gt; va conţine pe prima linie numărul &amp;lt;code&amp;gt;z&amp;lt;/code&amp;gt;, prin care este identificat nodul aflat la cea mai mare distanţã minimă faţã de nodul &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;0 &amp;lt; n &amp;lt; 100&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;0 &amp;lt; m &amp;lt; 1000&amp;lt;/code&amp;gt;&lt;br /&gt;
* Dacă există mai multe noduri aflate la distanţa maximă, poate fi ales oricare dintre ele.&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1 =&lt;br /&gt;
&amp;lt;code&amp;gt;dmaxIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 6 7&lt;br /&gt;
 1 3&lt;br /&gt;
 1 2&lt;br /&gt;
 2 3&lt;br /&gt;
 2 4&lt;br /&gt;
 3 4&lt;br /&gt;
 4 5&lt;br /&gt;
 5 6&lt;br /&gt;
&amp;lt;code&amp;gt;dmaxOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 6&lt;br /&gt;
&lt;br /&gt;
= Explicație =&lt;br /&gt;
Nodul &amp;lt;code&amp;gt;6&amp;lt;/code&amp;gt; se află la distanţa minimă &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt; faţă de nodul &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
&amp;lt;code&amp;gt;dmaxIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 101 1001&lt;br /&gt;
&amp;lt;code&amp;gt;dmaxOUT.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;python3&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
import heapq&lt;br /&gt;
&lt;br /&gt;
def dijkstra(graf, start):&lt;br /&gt;
    n = len(graf)&lt;br /&gt;
    distante = [float(&amp;#039;inf&amp;#039;)] * n&lt;br /&gt;
    distante[start - 1] = 0&lt;br /&gt;
&lt;br /&gt;
    heap = [(0, start)]&lt;br /&gt;
&lt;br /&gt;
    while heap:&lt;br /&gt;
        distanta_curenta, nod_curent = heapq.heappop(heap)&lt;br /&gt;
&lt;br /&gt;
        if distanta_curenta &amp;gt; distante[nod_curent - 1]:&lt;br /&gt;
            continue&lt;br /&gt;
&lt;br /&gt;
        for vecin in graf[nod_curent]:&lt;br /&gt;
            distanta = distanta_curenta + 1&lt;br /&gt;
            if distanta &amp;lt; distante[vecin - 1]:&lt;br /&gt;
                distante[vecin - 1] = distanta&lt;br /&gt;
                heapq.heappush(heap, (distanta, vecin))&lt;br /&gt;
&lt;br /&gt;
    return distante&lt;br /&gt;
&lt;br /&gt;
def verificare_restrictii(n, m):&lt;br /&gt;
    if 0 &amp;lt; n &amp;lt; 100 and 0 &amp;lt; m &amp;lt; 1000:&lt;br /&gt;
        return True&lt;br /&gt;
    return False&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;dmaxIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as fisier:&lt;br /&gt;
        n, m = map(int, fisier.readline().split())&lt;br /&gt;
&lt;br /&gt;
        if not verificare_restrictii(n, m):&lt;br /&gt;
            with open(&amp;quot;dmaxOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_iesire:&lt;br /&gt;
                fisier_iesire.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
            return&lt;br /&gt;
&lt;br /&gt;
        graf = {i: [] for i in range(1, n + 1)}&lt;br /&gt;
&lt;br /&gt;
        for _ in range(m):&lt;br /&gt;
            x, y = map(int, fisier.readline().split())&lt;br /&gt;
            graf[x].append(y)&lt;br /&gt;
            graf[y].append(x)&lt;br /&gt;
&lt;br /&gt;
    distante = dijkstra(graf, 1)&lt;br /&gt;
    nod_max_distanta = max(range(1, n + 1), key=lambda x: distante[x - 1])&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;dmaxOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_iesire:&lt;br /&gt;
        fisier_iesire.write(str(nod_max_distanta))&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>Simina</name></author>
	</entry>
</feed>