結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー uni_pythonuni_python
提出日時 2020-10-05 22:37:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 999 ms / 2,000 ms
コード長 3,902 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 87,376 KB
実行使用メモリ 148,044 KB
最終ジャッジ日時 2023-09-27 04:33:24
合計ジャッジ時間 27,720 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,380 KB
testcase_01 AC 76 ms
71,572 KB
testcase_02 AC 582 ms
112,324 KB
testcase_03 AC 725 ms
147,376 KB
testcase_04 AC 713 ms
146,648 KB
testcase_05 AC 479 ms
148,044 KB
testcase_06 AC 482 ms
147,876 KB
testcase_07 AC 178 ms
94,532 KB
testcase_08 AC 361 ms
141,112 KB
testcase_09 AC 133 ms
83,252 KB
testcase_10 AC 223 ms
106,232 KB
testcase_11 AC 186 ms
96,728 KB
testcase_12 AC 184 ms
98,100 KB
testcase_13 AC 697 ms
127,164 KB
testcase_14 AC 783 ms
132,768 KB
testcase_15 AC 894 ms
141,088 KB
testcase_16 AC 517 ms
113,036 KB
testcase_17 AC 976 ms
147,520 KB
testcase_18 AC 408 ms
103,820 KB
testcase_19 AC 879 ms
141,380 KB
testcase_20 AC 335 ms
95,624 KB
testcase_21 AC 456 ms
111,368 KB
testcase_22 AC 857 ms
137,252 KB
testcase_23 AC 95 ms
77,148 KB
testcase_24 AC 103 ms
79,724 KB
testcase_25 AC 170 ms
92,008 KB
testcase_26 AC 511 ms
113,548 KB
testcase_27 AC 579 ms
116,804 KB
testcase_28 AC 893 ms
141,400 KB
testcase_29 AC 232 ms
88,372 KB
testcase_30 AC 893 ms
140,824 KB
testcase_31 AC 621 ms
122,392 KB
testcase_32 AC 473 ms
107,588 KB
testcase_33 AC 999 ms
141,244 KB
testcase_34 AC 434 ms
103,808 KB
testcase_35 AC 885 ms
143,928 KB
testcase_36 AC 113 ms
78,860 KB
testcase_37 AC 145 ms
79,428 KB
testcase_38 AC 110 ms
79,076 KB
testcase_39 AC 141 ms
79,220 KB
testcase_40 AC 88 ms
76,664 KB
testcase_41 AC 939 ms
142,448 KB
testcase_42 AC 373 ms
98,264 KB
testcase_43 AC 541 ms
113,588 KB
testcase_44 AC 285 ms
91,280 KB
testcase_45 AC 519 ms
111,988 KB
testcase_46 AC 75 ms
71,424 KB
testcase_47 AC 75 ms
71,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7

    ##########################################
    import heapq
    class Dijkstra():
        """
        ・有向 / 無向は問わない(無向の場合は,逆向きの辺もたす)
        ・負のコストがない場合のみ
        ・計算量はO(E log|V|) 
        ・heapを使うことで頂点を走査する必要がなくなる(代わりに,距離更新したものは確定でなくともqueに入れておく)
        ・復元なし
        """

        #最短のpathをたす


        class Edge():
            #重み付き有向辺

            def __init__(self, _to, _cost):
                self.to =_to
                self.cost = _cost

        def __init__(self, V):
            #引数Vは頂点数
            self.inf=10**20
            self.G = [[] for _ in range(V)] #隣接リストG[u][i]が頂点uのi番目の辺
            self. _E = 0 #辺の数
            self._V = V #頂点数

        #proparty - 辺の数
        def E(self):
            return self._E

        #proparty - 頂点数
        def V(self):
            return self._V

        def add(self, _from, _to, _cost):
            #2頂点と辺のコストを追加
            self.G[_from].append(self.Edge(_to,_cost))
            self._E +=1

        def add2(self, _from, _to, _cost):
            #2頂点と辺のコスト(無向)を追加
            self.G[_from].append(self.Edge(_to, _cost))
            self.G[_to].append(self.Edge(_from, _cost))
            self._E +=2

        def shortest_path(self,s):#,g):
            #始点sから頂点iまでの最短経路長のリストを返す
            que = [] #priority queue
            d = [self.inf] * self.V()

            #prev = [None]*self.V() #prev[j]は,sからjへ最短経路で行くときのjの一つ前の場所
            #復元で使う

            d[s] = 0
            heapq.heappush(que,(0,s)) #始点の距離と頂点番号をヒープに追加

            while len(que)!=0:
                #キューに格納されてある中で一番コストが小さい頂点を取り出す
                cost,v = heapq.heappop(que)

                #キューに格納された最短経路長候補がdの距離よりも大きい場合に処理をスキップ
                if d[v] < cost:
                    continue

                #頂点vに隣接する各頂点iに対して,vを経由した場合の距離を計算して,これがd[i]よりも小さい場合に更新
                for i in range(len(self.G[v])):
                    e = self.G[v][i] #vのi個目の隣接辺
                    if d[e.to] > d[v] + e.cost:
                        d[e.to] = d[v] + e.cost #更新

                        #prev[e.to] = v
                        #復元で使う

                        heapq.heappush(que,(d[e.to],e.to)) #queに新たな最短経路長候補を追加

            """#sからgまでの最短経路
            path = []
            pos = g #今いる場所,ゴールで初期化
            for _ in range(self.V()+1):
                path.append(pos)
                if pos == s:
                    break
                #print("pos:",format(pos))
                pos = prev[pos]
            path.reverse()
            #print(path)"""

            return d#,path
    ########################
    N,M=MI()
    djk=Dijkstra(N)
    
    X,Y=MI()
    X-=1
    Y-=1
    p=[0]*N
    q=[0]*N
    for i in range(N):
        p[i],q[i]=MI()
        
    for i in range(M):
        P,Q=MI()
        P-=1
        Q-=1
        d=(p[P]-p[Q])**2 + (q[P]-q[Q])**2
        d=d**0.5
        
        djk.add2(P,Q,d)
        
    d=djk.shortest_path(X)
    ans=d[Y]
    
    print(ans)


main()
0