結果

問題 No.17 2つの地点に泊まりたい
ユーザー AreTrashAreTrash
提出日時 2016-09-26 20:13:53
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 3,645 bytes
コンパイル時間 995 ms
コンパイル使用メモリ 111,092 KB
実行使用メモリ 26,296 KB
最終ジャッジ日時 2024-04-23 02:16:06
合計ジャッジ時間 2,752 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
24,376 KB
testcase_01 AC 27 ms
23,920 KB
testcase_02 AC 27 ms
24,428 KB
testcase_03 AC 29 ms
24,248 KB
testcase_04 AC 27 ms
24,124 KB
testcase_05 AC 28 ms
24,180 KB
testcase_06 AC 27 ms
24,428 KB
testcase_07 AC 27 ms
24,168 KB
testcase_08 AC 28 ms
24,048 KB
testcase_09 AC 29 ms
26,168 KB
testcase_10 AC 28 ms
26,296 KB
testcase_11 AC 28 ms
25,912 KB
testcase_12 AC 28 ms
24,120 KB
testcase_13 AC 27 ms
24,172 KB
testcase_14 AC 26 ms
24,172 KB
testcase_15 AC 26 ms
26,172 KB
testcase_16 AC 26 ms
24,176 KB
testcase_17 AC 26 ms
24,052 KB
testcase_18 AC 27 ms
24,304 KB
testcase_19 AC 27 ms
26,044 KB
testcase_20 AC 26 ms
24,120 KB
testcase_21 AC 27 ms
26,212 KB
testcase_22 AC 26 ms
24,172 KB
testcase_23 AC 27 ms
24,236 KB
testcase_24 AC 28 ms
23,868 KB
testcase_25 AC 26 ms
26,172 KB
testcase_26 AC 29 ms
24,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

namespace No17_1{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var N = sr.Next<int>();
            var S = sr.Next<int>(N);

            var M = sr.Next<int>();
            var wf = new WarshallFloyd(N);
            for(var i = 0; i < M; i++){
                var A = sr.Next<int>();
                var B = sr.Next<int>();
                var C = sr.Next<int>();
                wf.AddEdge(A, B, C);
                wf.AddEdge(B, A, C);
            }

            var dist = wf.Solve();
            var res = int.MaxValue;
            for(var i = 1; i < N - 1; i++){
                for(var j = 1; j < N - 1; j++){
                    if(i == j) continue;
                    if(dist[0][i] == int.MaxValue || dist[i][j] == int.MaxValue || dist[j][N - 1] == int.MaxValue) continue;
                    res = Math.Min(res, dist[0][i] + S[i] + dist[i][j] + S[j] + dist[j][N - 1]);
                }
            }

            Console.WriteLine(res);
            //---------------------------------
        }
    }

    public class WarshallFloyd{
        private readonly int _v;
        private readonly int[][] _dist;

        public WarshallFloyd(int v){
            _v = v;
            _dist = new int[v][];
            for(var i = 0; i < v; i++){
                _dist[i] = new int[v];
            }

            for(var i = 0; i < v; i++){
                for(var j = 0; j < v; j++){
                    if(i != j) _dist[i][j] = int.MaxValue;
                }
            }
        }

        public void AddEdge(int from, int to, int cost){
            _dist[from][to] = cost;
        }

        public int[][] Solve(){
            for(var k = 0; k < _v; k++){
                for(var i = 0; i < _v; i++){
                    for(var j = 0; j < _v; j++){
                        if(_dist[i][k] != int.MaxValue && _dist[k][j] != int.MaxValue){
                            _dist[i][j] = Math.Min(_dist[i][j], _dist[i][k] + _dist[k][j]);
                        }
                    }
                }
            }

            for(var i = 0; i < _v; i++){
                if(_dist[i][i] < 0) return null;
            }

            return _dist;
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}
0