結果

問題 No.1344 Typical Shortest Path Sum
ユーザー marcypy
提出日時 2021-09-08 07:36:31
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 2,151 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-12-24 21:03:30
合計ジャッジ時間 10,817 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 14 WA * 63
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
#input = sys.stdin.readline
fn=lambda:int(input())
f=lambda:input().split()
ff=lambda:map(int,input().split())
fff=lambda:list(map(int,input().split()))
from collections import Counter,deque,defaultdict
from itertools import combinations,permutations
import re,math,heapq,bisect
ma=-float('inf')
mi=float('inf')
mod=10**9+7
dis=((1,0),(-1,0),(0,1),(0,-1))
#import numpy as np #pypyでは使えない
##############################################


class WarshallFloyd():
    def __init__(self, N):
        self.N = N
        self.d = [[float("inf") for i in range(N)]
                  for i in range(N)]  # d[u][v] : 辺uvのコスト(存在しないときはinf)

    def add(self, u, v, c, directed=False):
        """
        0-indexedであることに注意
        u = from, v = to, c = cost
        directed = Trueなら、有向グラフである
        """
        if directed is False:
            self.d[u][v] = c
            self.d[v][u] = c
        else:
            self.d[u][v] = c

    def WarshallFloyd_search(self):
        # これを d[i][j]: iからjへの最短距離 にする
        # 本来無向グラフでのみ全域木を考えるが、二重辺なら有向でも行けそう
        # d[i][i] < 0 なら、グラフは負のサイクルを持つ
        
        for k in range(self.N):
            for i in range(self.N):
                for j in range(self.N):
                    self.d[i][j]=min(self.d[i][j],self.d[i][k] + self.d[k][j])

        hasNegativeCycle = False
        for i in range(self.N):
            if self.d[i][i] < 0:
                hasNegativeCycle = True
                break
        for i in range(self.N):
            self.d[i][i] = 0
        return hasNegativeCycle, self.d
     

N,M=ff()
std=[fff() for i in range(M)]
graph = WarshallFloyd(N)

for s,t,d in std:
    graph.add(s-1,t-1,d,True)
_,D = graph.WarshallFloyd_search()

for i in range(N):
    ans=0
    for j in range(N):
        if D[i][j]=='inf':
            continue
        else:
            ans+=D[i][j]
    print(ans)

#サンプル3だめ 多重辺の処理が必要みたい
0