結果

問題 No.17 2つの地点に泊まりたい
ユーザー 14番14番
提出日時 2016-03-31 17:40:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 4,225 bytes
コンパイル時間 3,043 ms
コンパイル使用メモリ 105,916 KB
実行使用メモリ 22,776 KB
最終ジャッジ日時 2023-08-05 03:45:24
合計ジャッジ時間 5,407 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
22,728 KB
testcase_01 AC 63 ms
20,712 KB
testcase_02 AC 64 ms
20,624 KB
testcase_03 AC 65 ms
20,904 KB
testcase_04 AC 67 ms
20,692 KB
testcase_05 AC 84 ms
22,768 KB
testcase_06 AC 75 ms
20,780 KB
testcase_07 AC 72 ms
20,740 KB
testcase_08 AC 100 ms
22,704 KB
testcase_09 AC 100 ms
22,688 KB
testcase_10 AC 73 ms
20,728 KB
testcase_11 AC 82 ms
20,780 KB
testcase_12 AC 64 ms
20,716 KB
testcase_13 AC 63 ms
20,708 KB
testcase_14 AC 64 ms
20,720 KB
testcase_15 AC 64 ms
20,732 KB
testcase_16 AC 65 ms
20,772 KB
testcase_17 AC 65 ms
20,632 KB
testcase_18 AC 66 ms
20,720 KB
testcase_19 AC 65 ms
20,660 KB
testcase_20 AC 64 ms
22,776 KB
testcase_21 AC 64 ms
20,808 KB
testcase_22 AC 67 ms
22,732 KB
testcase_23 AC 81 ms
20,852 KB
testcase_24 AC 80 ms
20,632 KB
testcase_25 AC 64 ms
22,680 KB
testcase_26 AC 92 ms
20,792 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
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 = 0;
                    int cost1 = GetCost(0, i);
                    int cost2 = GetCost(i, j);
                    int cost3 = GetCost(j, matiCount - 1);
                    if(cost1 >= 0 && cost2 >= 0 && cost3 >= 0) {
                        subTotal = cost1 + cost2 + cost3 + 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();
            if(this.road.ContainsKey(pos)) {
                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) {
                int val = -1;
                if(localDic[i] != null) {
                    val = localDic[i].Value;
                }
                moveDic[fromMati][i] = val;
                if(!moveDic.ContainsKey(i)) {
                    moveDic.Add(i, new Dictionary<int, int>());
                    moveDic[i][fromMati] = val;
                }
            }
        }
        return (localDic[toMati] == null)?(-1):(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();
    }
}
0