結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー さかぽん
提出日時 2020-12-25 23:41:28
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 1,321 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 112,824 KB
実行使用メモリ 28,292 KB
最終ジャッジ日時 2024-09-22 17:13:32
合計ジャッジ時間 4,319 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 RE * 1
other AC * 20 WA * 9 RE * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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)
		{
			throw new NotImplementedException();
		}
		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;
	}
}
0