結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2016-03-31 17:19:30 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,844 bytes |
| コンパイル時間 | 916 ms |
| コンパイル使用メモリ | 113,416 KB |
| 実行使用メモリ | 24,060 KB |
| 最終ジャッジ日時 | 2024-10-02 08:29:50 |
| 合計ジャッジ時間 | 2,948 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 RE * 10 |
コンパイルメッセージ
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;
using System.Collections.Generic;
using System.Text;
public class Program
{
public void Proc() {
Reader.IsDebug = false;
int matiCount = int.Parse(Reader.ReadLine());
this.Taizaihi = new int[matiCount];
for(int i=0; i<matiCount; i++) {
this.Taizaihi[i] = int.Parse(Reader.ReadLine());
}
int roadCount = int.Parse(Reader.ReadLine());
for(int i=0; i<roadCount; i++) {
int[] inpt = Reader.GetInt();
if(!road.ContainsKey(inpt[0])) {
road.Add(inpt[0], new Dictionary<int, int>());
}
road[inpt[0]].Add(inpt[1], inpt[2]);
if(!road.ContainsKey(inpt[1])) {
road.Add(inpt[1], new Dictionary<int, int>());
}
road[inpt[1]].Add(inpt[0], inpt[2]);
}
int ans = int.MaxValue ;
for(int i=1; i<matiCount - 1; i++) {
for(int j=1; j<matiCount - 1; j++) {
if(i != j) {
int subTotal = GetCost(0, i);
subTotal += GetCost(i, j);
subTotal += GetCost(j, matiCount - 1);
subTotal += Taizaihi[i] + Taizaihi[j];
ans = Math.Min(ans, subTotal);
}
}
}
Console.WriteLine(ans);
}
Dictionary<int, Dictionary<int, int>> moveDic = new Dictionary<int, Dictionary<int, int>>();
private int GetCost(int fromMati, int toMati) {
if(!moveDic.ContainsKey(fromMati)) {
moveDic.Add(fromMati, new Dictionary<int, int>());
}
if(moveDic[fromMati].ContainsKey(toMati)) {
return moveDic[fromMati][toMati];
}
Nullable<int>[] localDic = new Nullable<int>[this.Taizaihi.Length];
Queue<int> task = new Queue<int>();
task.Enqueue(fromMati);
localDic[fromMati] = 0;
while (task.Count> 0)
{
int pos = task.Dequeue();
foreach (int key in this.road[pos].Keys)
{
if(localDic[key] == null || localDic[key].Value > localDic[pos].Value + road[pos][key]) {
localDic[key] = localDic[pos].Value + road[pos][key];
task.Enqueue(key);
}
}
}
for(int i=0; i<localDic.Length; i++) {
if(fromMati != i) {
moveDic[fromMati][i] = localDic[i].Value;
if(!moveDic.ContainsKey(i)) {
moveDic.Add(i, new Dictionary<int, int>());
moveDic[i][fromMati] = localDic[i].Value;
}
}
}
return localDic[toMati].Value;
}
private Dictionary<int, Dictionary<int, int>> road = new Dictionary<int, Dictionary<int, int>>();
private int[] Taizaihi;
public class Reader {
private static String InitText = @"
";
private static System.IO.StringReader sr = null;
public static bool IsDebug = true;
public static string ReadLine() {
if(IsDebug) {
if(sr == null) {
sr = new System.IO.StringReader(InitText.Trim());
}
return sr.ReadLine();
} else {
return Console.ReadLine();
}
}
public static int[] GetInt(char delimiter = ' ') {
string[] inpt = ReadLine().Split(delimiter);
int[] ret = new int[inpt.Length];
for(int i=0; i<inpt.Length; i++) {
ret[i] = int.Parse(inpt[i]);
}
return ret;
}
}
public static void Main(string[] args)
{
Program prg = new Program();
prg.Proc();
}
}
14番