<?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=4078_-_Distanta</id>
	<title>4078 - Distanta - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4078_-_Distanta"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4078_-_Distanta&amp;action=history"/>
	<updated>2026-05-01T12:34:31Z</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=4078_-_Distanta&amp;diff=9135&amp;oldid=prev</id>
		<title>Rus Marius: Pagină nouă: = Cerința = Matei, care locuiește in orașul &lt;code&gt;p&lt;/code&gt;, vrea să ajungă in orașul &lt;code&gt;q&lt;/code&gt; să il viziteze. El are o hartă pe care se află &lt;code&gt;n&lt;/code&gt; orașe și &lt;code&gt;m&lt;/code&gt; drumuri prin care poate să treacă pentru a ajunge la destinație. Pe hartă apare și durata de timp &lt;code&gt;td&lt;/code&gt; pentru fiecare drum, care reprezintă numărul de minute în care este parcurs drumul și &lt;code&gt;to&lt;/code&gt; pentru traversarea orașelor (unele orașe sunt mai aglome...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4078_-_Distanta&amp;diff=9135&amp;oldid=prev"/>
		<updated>2024-01-06T19:49:40Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerința = Matei, care locuiește in orașul &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;, vrea să ajungă in orașul &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; să il viziteze. El are o hartă pe care se află &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; orașe și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; drumuri prin care poate să treacă pentru a ajunge la destinație. Pe hartă apare și durata de timp &amp;lt;code&amp;gt;td&amp;lt;/code&amp;gt; pentru fiecare drum, care reprezintă numărul de minute în care este parcurs drumul și &amp;lt;code&amp;gt;to&amp;lt;/code&amp;gt; pentru traversarea orașelor (unele orașe sunt mai aglome...&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;
Matei, care locuiește in orașul &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;, vrea să ajungă in orașul &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; să il viziteze. El are o hartă pe care se află &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; orașe și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; drumuri prin care poate să treacă pentru a ajunge la destinație. Pe hartă apare și durata de timp &amp;lt;code&amp;gt;td&amp;lt;/code&amp;gt; pentru fiecare drum, care reprezintă numărul de minute în care este parcurs drumul și &amp;lt;code&amp;gt;to&amp;lt;/code&amp;gt; pentru traversarea orașelor (unele orașe sunt mai aglomerate, iar altele nu). Matei vrea să ajungă cat mai rapid la destinație, dar dacă există mai multe trasee de timp minim, il va alege pe acela care trece prin mai multe orașe. Nu vor exista trasee de timp minim cu același număr de orașe. Ajutati-l pe Matei să gasească drumul cerut.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;distantaIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numărul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, iar pe următoarele &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; linii se citesc &amp;lt;code&amp;gt;nume oraș&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;to&amp;lt;/code&amp;gt; al acestuia. Se citește &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, iar pe următoarele &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; linii &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;td&amp;lt;/code&amp;gt; unde &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt; reprezintă orașele care sunt legate prin drumul care se parcurge în &amp;lt;code&amp;gt;td&amp;lt;/code&amp;gt;. Pe ultima linie se citesc &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;q&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;distantaOUT.txt&amp;lt;/code&amp;gt; va conține pe prima linie numărul &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;, reprezentând durata de timp în care se parcurge traseul cerut, iar pe următoarele linii se afișează traseul. Dacă nu există nici un traseu se va afișa &amp;lt;code&amp;gt;NU EXISTA TRASEU&amp;lt;/code&amp;gt;.Î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 &amp;lt; n &amp;lt; 21&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;0 ≤ m ≤ 190&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;0 &amp;lt; numele orașelor (număr caractere) &amp;lt; 21&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;0 ≤ numărul de drumuri dintre oricare 2 orașe ≤ 1&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;0 ≤ S ≤ 2.000.000.001&amp;lt;/code&amp;gt;&lt;br /&gt;
* Nu există drum de la un oraș la el însuși&lt;br /&gt;
* Dacă nu există nici un traseu se va afișa &amp;lt;code&amp;gt;NU EXISTA TRASEU&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;distantaIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 7 &lt;br /&gt;
 Bistrita 14&lt;br /&gt;
 Oradea 80&lt;br /&gt;
 Beius 7&lt;br /&gt;
 Galatii-Bistritei 1&lt;br /&gt;
 Reghin 3&lt;br /&gt;
 Sibiu 15&lt;br /&gt;
 Cluj-Napoca 30&lt;br /&gt;
 10&lt;br /&gt;
 Bistrita Oradea 223&lt;br /&gt;
 Cluj-Napoca Sibiu 119&lt;br /&gt;
 Bistrita Cluj-Napoca 100&lt;br /&gt;
 Beius Oradea 50&lt;br /&gt;
 Bistrita Galatii-Bistritei 15&lt;br /&gt;
 Reghin Galatii-Bistritei 35&lt;br /&gt;
 Oradea Cluj-Napoca 100&lt;br /&gt;
 Sibiu Reghin 154&lt;br /&gt;
 Beius Sibiu 130&lt;br /&gt;
 Reghin Cluj-Napoca 133&lt;br /&gt;
 Bistrita Beius&lt;br /&gt;
&amp;lt;code&amp;gt;distantaOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 374&lt;br /&gt;
 Bistrita&lt;br /&gt;
 Galatii-Bistritei&lt;br /&gt;
 Reghin&lt;br /&gt;
 Sibiu&lt;br /&gt;
 Beius&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;distantaIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 22&lt;br /&gt;
 Bistrita 14&lt;br /&gt;
 Oradea 80&lt;br /&gt;
 Beius 7&lt;br /&gt;
 Galatii-Bistritei 1&lt;br /&gt;
 Reghin 3&lt;br /&gt;
 Sibiu 15&lt;br /&gt;
 Cluj-Napoca 30&lt;br /&gt;
 10&lt;br /&gt;
 Bistrita Oradea 223&lt;br /&gt;
 Cluj-Napoca Sibiu 119&lt;br /&gt;
 Bistrita Cluj-Napoca 100&lt;br /&gt;
 Beius Oradea 50&lt;br /&gt;
 Bistrita Galatii-Bistritei 15&lt;br /&gt;
 Reghin Galatii-Bistritei 35&lt;br /&gt;
 Oradea Cluj-Napoca 100&lt;br /&gt;
 Sibiu Reghin 154&lt;br /&gt;
 Beius Sibiu 130&lt;br /&gt;
 Reghin Cluj-Napoca 133&lt;br /&gt;
 Bistrita Beius&lt;br /&gt;
&amp;lt;code&amp;gt;distantaOUT.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;
from queue import Queue&lt;br /&gt;
&lt;br /&gt;
Inf = 0x3f3f3f3f&lt;br /&gt;
&lt;br /&gt;
def check_restrictions(n, m, codif):&lt;br /&gt;
    if not (1 &amp;lt; n &amp;lt; 21) or not (0 &amp;lt;= m &amp;lt;= 190):&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
    for _, _, s in codif:&lt;br /&gt;
        if not (0 &amp;lt; len(s) &amp;lt; 21):&lt;br /&gt;
            return False&lt;br /&gt;
&lt;br /&gt;
    return True&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    with open(&amp;quot;distantaIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as infile, open(&amp;quot;distantaOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as outfile:&lt;br /&gt;
        n = int(infile.readline().strip())&lt;br /&gt;
        codif = []&lt;br /&gt;
        G = [[] for _ in range(51)]&lt;br /&gt;
        t = [0] * 51&lt;br /&gt;
        timp = [0] * 51&lt;br /&gt;
&lt;br /&gt;
        if not (1 &amp;lt; n &amp;lt; 21):&lt;br /&gt;
            outfile.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        else:&lt;br /&gt;
            for i in range(1, n + 1):&lt;br /&gt;
                line = infile.readline().split()&lt;br /&gt;
                if len(line) != 2:&lt;br /&gt;
                    outfile.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
                    break&lt;br /&gt;
&lt;br /&gt;
                s, cost = line&lt;br /&gt;
                cost = int(cost)&lt;br /&gt;
                codif.append((i, cost, s))&lt;br /&gt;
&lt;br /&gt;
            m = int(infile.readline().strip())&lt;br /&gt;
&lt;br /&gt;
            if not (0 &amp;lt;= m &amp;lt;= 190):&lt;br /&gt;
                outfile.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
            else:&lt;br /&gt;
                for i in range(1, m + 1):&lt;br /&gt;
                    line = infile.readline().split()&lt;br /&gt;
                    if len(line) != 3:&lt;br /&gt;
                        outfile.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
                        break&lt;br /&gt;
&lt;br /&gt;
                    unu, doi, cost = line&lt;br /&gt;
                    cost = int(cost)&lt;br /&gt;
                    x = next(item[0] for item in codif if item[2] == unu)&lt;br /&gt;
                    y = next(item[0] for item in codif if item[2] == doi)&lt;br /&gt;
                    G[x].append((y, cost))&lt;br /&gt;
                    G[y].append((x, cost))&lt;br /&gt;
&lt;br /&gt;
                unu, doi = infile.readline().split()&lt;br /&gt;
                start_node = next(item[0] for item in codif if item[2] == doi)&lt;br /&gt;
&lt;br /&gt;
                def decodif(oras):&lt;br /&gt;
                    for x, y, s in codif:&lt;br /&gt;
                        if s == oras:&lt;br /&gt;
                            return x&lt;br /&gt;
&lt;br /&gt;
                def costnod(nod):&lt;br /&gt;
                    for x, y, s in codif:&lt;br /&gt;
                        if nod == x:&lt;br /&gt;
                            return y&lt;br /&gt;
&lt;br /&gt;
                def afisare(nod):&lt;br /&gt;
                    for x, y, s in codif:&lt;br /&gt;
                        if nod == x:&lt;br /&gt;
                            return s&lt;br /&gt;
&lt;br /&gt;
                def bfs(nod):&lt;br /&gt;
                    t[nod] = nod&lt;br /&gt;
                    timp[nod] = costnod(nod)&lt;br /&gt;
                    q = Queue()&lt;br /&gt;
                    q.put(nod)&lt;br /&gt;
&lt;br /&gt;
                    while not q.empty():&lt;br /&gt;
                        x = q.get()&lt;br /&gt;
&lt;br /&gt;
                        for i, c in G[x]:&lt;br /&gt;
                            costnou = timp[x] + c + costnod(i)&lt;br /&gt;
&lt;br /&gt;
                            if not timp[i] or costnou &amp;lt;= timp[i]:&lt;br /&gt;
                                timp[i] = costnou&lt;br /&gt;
                                t[i] = x&lt;br /&gt;
                                q.put(i)&lt;br /&gt;
&lt;br /&gt;
                bfs(start_node)&lt;br /&gt;
&lt;br /&gt;
                if timp[decodif(unu)] != 0:&lt;br /&gt;
                    outfile.write(str(timp[decodif(unu)]) + &amp;#039;\n&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
                    plec = decodif(unu)&lt;br /&gt;
                    while plec != t[plec]:&lt;br /&gt;
                        outfile.write(afisare(plec) + &amp;#039;\n&amp;#039;)&lt;br /&gt;
                        plec = t[plec]&lt;br /&gt;
&lt;br /&gt;
                    outfile.write(afisare(decodif(doi)))&lt;br /&gt;
                else:&lt;br /&gt;
                    outfile.write(&amp;quot;NU EXISTA TRASEU&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rus Marius</name></author>
	</entry>
</feed>