<?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=4155_-_Harta3</id>
	<title>4155 - Harta3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4155_-_Harta3"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4155_-_Harta3&amp;action=history"/>
	<updated>2026-05-01T08:46:58Z</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=4155_-_Harta3&amp;diff=10089&amp;oldid=prev</id>
		<title>Danciu: Pagină nouă:  = Cerința = Harta unei regiuni este reprezentată într-un sistem de coordonate cartezian și sunt cunoscute coordonatele a &lt;code&gt;n&lt;/code&gt; orașe, numerotate de la &lt;code&gt;1&lt;/code&gt; la &lt;code&gt;n&lt;/code&gt;.  Se dorește construirea unor drumuri bidirecționale între anumite perechi de orașe, astfel încât:  * să se poate ajunge din orice oraș în oricare altul folosind unul sau mai multe dintre drumurile construite; * suma lungimilor drumurilor construite să fie minimă.  Să...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4155_-_Harta3&amp;diff=10089&amp;oldid=prev"/>
		<updated>2024-06-04T03:30:31Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă:  = Cerința = Harta unei regiuni este reprezentată într-un sistem de coordonate cartezian și sunt cunoscute coordonatele a &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; orașe, 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;.  Se dorește construirea unor drumuri bidirecționale între anumite perechi de orașe, astfel încât:  * să se poate ajunge din orice oraș în oricare altul folosind unul sau mai multe dintre drumurile construite; * suma lungimilor drumurilor construite să fie minimă.  Să...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
= Cerința =&lt;br /&gt;
Harta unei regiuni este reprezentată într-un sistem de coordonate cartezian și sunt cunoscute coordonatele a &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; orașe, 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;.&lt;br /&gt;
&lt;br /&gt;
Se dorește construirea unor drumuri bidirecționale între anumite perechi de orașe, astfel încât:&lt;br /&gt;
&lt;br /&gt;
* să se poate ajunge din orice oraș în oricare altul folosind unul sau mai multe dintre drumurile construite;&lt;br /&gt;
* suma lungimilor drumurilor construite să fie minimă.&lt;br /&gt;
&lt;br /&gt;
Să se determine suma lungimilor drumurilor construite.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;harta3.in&amp;lt;/code&amp;gt; conține pe prima linie numărul de orașe &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, iar pe fiecare dintre următoarele &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; linii câte două numere întregi &amp;lt;code&amp;gt;x y&amp;lt;/code&amp;gt;, reprezentând coordonatele acestora.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;harta3.out&amp;lt;/code&amp;gt; va conține pe prima linie suma lungimilor drumurilor construite, cu cel puțin trei zecimale exacte.&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;
* coordonatele orașelor sunt din intervalul &amp;lt;code&amp;gt;[-200, 200]&amp;lt;/code&amp;gt;;&lt;br /&gt;
* lungimea drumului dintre două orașe se determină cu formula , unde  și  sunt coordonatele acestora.&lt;br /&gt;
&lt;br /&gt;
= Exemplu: =&lt;br /&gt;
&amp;lt;code&amp;gt;harta3.in&amp;lt;/code&amp;gt;&lt;br /&gt;
 4&lt;br /&gt;
 1 2&lt;br /&gt;
 2 -1&lt;br /&gt;
 6 5&lt;br /&gt;
 4 2&lt;br /&gt;
&amp;lt;code&amp;gt;harta3.out&amp;lt;/code&amp;gt;&lt;br /&gt;
 9.7678&lt;br /&gt;
&lt;br /&gt;
== Explicație ==&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot;&amp;gt;&lt;br /&gt;
import math&lt;br /&gt;
&lt;br /&gt;
class Muchie:&lt;br /&gt;
    def __init__(self, i, j, cost):&lt;br /&gt;
        self.i = i&lt;br /&gt;
        self.j = j&lt;br /&gt;
        self.cost = cost&lt;br /&gt;
&lt;br /&gt;
def find(P, i):&lt;br /&gt;
    if P[i] == i:&lt;br /&gt;
        return i&lt;br /&gt;
    else:&lt;br /&gt;
        P[i] = find(P, P[i])&lt;br /&gt;
        return P[i]&lt;br /&gt;
&lt;br /&gt;
def union(P, rank, x, y):&lt;br /&gt;
    root_x = find(P, x)&lt;br /&gt;
    root_y = find(P, y)&lt;br /&gt;
    if root_x != root_y:&lt;br /&gt;
        if rank[root_x] &amp;gt; rank[root_y]:&lt;br /&gt;
            P[root_y] = root_x&lt;br /&gt;
        elif rank[root_x] &amp;lt; rank[root_y]:&lt;br /&gt;
            P[root_x] = root_y&lt;br /&gt;
        else:&lt;br /&gt;
            P[root_y] = root_x&lt;br /&gt;
            rank[root_x] += 1&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;harta3.in&amp;quot;, &amp;quot;r&amp;quot;) as fin:&lt;br /&gt;
        n = int(fin.readline().strip())&lt;br /&gt;
        x = [0] * (n + 1)&lt;br /&gt;
        y = [0] * (n + 1)&lt;br /&gt;
        for i in range(1, n + 1):&lt;br /&gt;
            x[i], y[i] = map(int, fin.readline().strip().split())&lt;br /&gt;
&lt;br /&gt;
    M = []&lt;br /&gt;
    for i in range(1, n):&lt;br /&gt;
        for j in range(i + 1, n + 1):&lt;br /&gt;
            cost = math.sqrt((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2)&lt;br /&gt;
            M.append(Muchie(i, j, cost))&lt;br /&gt;
    &lt;br /&gt;
    M.sort(key=lambda muchie: muchie.cost)&lt;br /&gt;
&lt;br /&gt;
    P = [i for i in range(n + 1)]&lt;br /&gt;
    rank = [0] * (n + 1)&lt;br /&gt;
    S = 0.0&lt;br /&gt;
&lt;br /&gt;
    for muchie in M:&lt;br /&gt;
        if find(P, muchie.i) != find(P, muchie.j):&lt;br /&gt;
            union(P, rank, muchie.i, muchie.j)&lt;br /&gt;
            S += muchie.cost&lt;br /&gt;
    &lt;br /&gt;
    with open(&amp;quot;harta3.out&amp;quot;, &amp;quot;w&amp;quot;) as fout:&lt;br /&gt;
        fout.write(f&amp;quot;{S:.4f}\n&amp;quot;)&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>Danciu</name></author>
	</entry>
</feed>