結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-25 23:40:15 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,282 bytes |
| コンパイル時間 | 1,072 ms |
| コンパイル使用メモリ | 113,660 KB |
| 実行使用メモリ | 28,136 KB |
| 最終ジャッジ日時 | 2024-09-22 17:11:42 |
| 合計ジャッジ時間 | 4,492 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 20 WA * 37 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
class Q
{
static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
static void Main()
{
var directed = int.Parse(Console.ReadLine()) == 1;
var (n, m) = Read2();
var es = Array.ConvertAll(new bool[m], _ => Read());
var map = EdgesToMap2(n + 1, es, directed);
var r = long.MaxValue;
var u = new bool[n + 1];
var costs = new long[n + 1];
if (directed)
{
}
else
{
for (int v = 1; v <= n; v++)
{
if (u[v]) continue;
DfsU(v, -1);
}
Console.WriteLine(r == long.MaxValue ? -1 : r);
}
void DfsU(int v, int pv)
{
u[v] = true;
foreach (var e in map[v])
{
var nv = e[1];
if (nv == pv) continue;
if (u[nv])
{
if (costs[v] > costs[nv])
r = Math.Min(r, costs[v] - costs[nv] + e[2]);
continue;
}
costs[nv] = costs[v] + e[2];
DfsU(nv, v);
}
}
}
static List<int[]>[] EdgesToMap2(int n, int[][] es, bool directed)
{
var map = Array.ConvertAll(new bool[n], _ => new List<int[]>());
foreach (var e in es)
{
map[e[0]].Add(new[] { e[0], e[1], e[2] });
if (!directed) map[e[1]].Add(new[] { e[1], e[0], e[2] });
}
return map;
}
}