結果
| 問題 |
No.1344 Typical Shortest Path Sum
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-16 13:32:51 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 34 ms / 2,000 ms |
| コード長 | 1,450 bytes |
| コンパイル時間 | 1,115 ms |
| コンパイル使用メモリ | 114,716 KB |
| 実行使用メモリ | 27,576 KB |
| 最終ジャッジ日時 | 2024-11-27 13:59:15 |
| 合計ジャッジ時間 | 4,796 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 77 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Linq;
class C
{
static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
static long[] ReadL() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
static void Main()
{
var (n, m) = Read2();
var es = Array.ConvertAll(new bool[m], _ => ReadL());
var r = WarshallFloyd(n + 1, es, true).Item1;
Console.WriteLine(string.Join("\n", r.Skip(1).Select(vs => vs.Where(v => v != long.MaxValue).Sum())));
}
public static Tuple<long[][], int[][]> WarshallFloyd(int n, long[][] es, bool directed)
{
var cs = Array.ConvertAll(new bool[n], i => Array.ConvertAll(new bool[n], _ => long.MaxValue));
var inters = Array.ConvertAll(new bool[n], i => Array.ConvertAll(new bool[n], _ => -1));
for (int i = 0; i < n; ++i) cs[i][i] = 0;
foreach (var e in es)
{
cs[e[0]][e[1]] = Math.Min(cs[e[0]][e[1]], e[2]);
if (!directed) cs[e[1]][e[0]] = Math.Min(cs[e[1]][e[0]], e[2]);
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
if (cs[i][k] == long.MaxValue || cs[k][j] == long.MaxValue) continue;
var nc = cs[i][k] + cs[k][j];
if (cs[i][j] <= nc) continue;
cs[i][j] = nc;
inters[i][j] = k;
}
for (int i = 0; i < n; ++i) if (cs[i][i] < 0) return Tuple.Create<long[][], int[][]>(null, null);
return Tuple.Create(cs, inters);
}
}