結果

問題 No.1283 Extra Fee
ユーザー NoneNone
提出日時 2021-10-25 15:18:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 941 ms / 2,000 ms
コード長 3,181 bytes
コンパイル時間 435 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 145,072 KB
最終ジャッジ日時 2023-08-10 07:08:03
合計ジャッジ時間 14,616 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,264 KB
testcase_01 AC 70 ms
71,532 KB
testcase_02 AC 71 ms
71,372 KB
testcase_03 AC 74 ms
71,620 KB
testcase_04 AC 73 ms
71,304 KB
testcase_05 AC 71 ms
71,304 KB
testcase_06 AC 73 ms
71,364 KB
testcase_07 AC 70 ms
71,380 KB
testcase_08 AC 75 ms
71,152 KB
testcase_09 AC 73 ms
71,308 KB
testcase_10 AC 74 ms
71,524 KB
testcase_11 AC 212 ms
81,760 KB
testcase_12 AC 182 ms
81,780 KB
testcase_13 AC 163 ms
80,584 KB
testcase_14 AC 241 ms
89,056 KB
testcase_15 AC 313 ms
94,756 KB
testcase_16 AC 172 ms
81,968 KB
testcase_17 AC 846 ms
140,192 KB
testcase_18 AC 672 ms
132,864 KB
testcase_19 AC 703 ms
135,096 KB
testcase_20 AC 650 ms
131,996 KB
testcase_21 AC 669 ms
132,656 KB
testcase_22 AC 612 ms
126,504 KB
testcase_23 AC 747 ms
137,224 KB
testcase_24 AC 752 ms
136,200 KB
testcase_25 AC 774 ms
136,956 KB
testcase_26 AC 730 ms
136,956 KB
testcase_27 AC 753 ms
136,540 KB
testcase_28 AC 761 ms
136,736 KB
testcase_29 AC 941 ms
145,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Graph:

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append((y, cost))
        if self.directed == False:
            self.edges[y].append((x, cost))

    def dijkstra(self, start=None, INF=10**18):
        """
        返り値は 0-indexed !!!
        :param start: スタート地点
        :return: スタート地点から各点への距離のリスト
        備考: heqpq の比較のための key は第一引数である点に注意( = heappush(heapq, (key,value)) )
        """
        res = [INF] * self.n
        if start is None: start=self.decrement
        start=start-self.decrement
        res[start] = 0
        next_set = [(0, start)]
        while next_set:
            dist, p = heappop(next_set)
            if res[p] < dist:
                continue
            """ここで頂点pまでの最短距離が確定。よって、ここを通るのはN回のみ"""
            for q, cost in self.edges[p]:
                temp_d = dist + cost
                if temp_d < res[q]:
                    res[q] = temp_d
                    heappush(next_set, (temp_d, q))

        return res

    def draw(self):
        """
        :return: グラフを可視化
        """
        import matplotlib.pyplot as plt
        import networkx as nx

        if self.directed:
            G = nx.DiGraph()
        else:
            G = nx.Graph()
        for x in range(self.n):
            for y, cost in self.edges[x]:
                G.add_edge(x + self.decrement, y + self.decrement, weight=cost)


        edge_labels = {(i, j): w['weight'] for i, j, w in G.edges(data=True)}
        pos = nx.spring_layout(G)
        nx.draw_networkx(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.1')
        nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
        plt.axis("off")
        plt.show()

#########################################################
def example():
    global input
    example = iter(
        """
3 1
2 2 3
        """
            .strip().split("\n"))
    input = lambda: next(example)



#########################################################
import sys
from heapq import *
input = sys.stdin.readline

# example()

INF = 10**18  # 大きい数字

N,M = map(int, input().split())

graph = Graph(N**2, directed=True, decrement=False)

mat=[[0]*N for _ in range(N)]
for _ in range(M):
    h,w,c=map(int, input().split())
    h-=1
    w-=1
    mat[h][w]=c

for h0 in range(N):
    for w0 in range(N):
        for dh,dw in ((-1,0),(1,0),(0,-1),(0,1)):
            h,w=h0+dh,w0+dw
            if 0<=h<N and 0<=w<N:
                graph.add_edge(h0*N+w0,h*N+w,1+mat[h][w])

dist0=graph.dijkstra(start=0,INF=INF)
dist1=graph.dijkstra(start=N**2-1,INF=INF)

d=INF
for h in range(N):
    for w in range(N):
        d=min(dist0[h*N+w]+dist1[h*N+w]-2*mat[h][w],d)
print(d)
0