結果

問題 No.17 2つの地点に泊まりたい
ユーザー AreTrashAreTrash
提出日時 2016-09-03 18:44:52
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 5,739 bytes
コンパイル時間 5,511 ms
コンパイル使用メモリ 112,512 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-11-15 19:18:11
合計ジャッジ時間 5,755 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
19,584 KB
testcase_01 AC 31 ms
19,328 KB
testcase_02 AC 31 ms
19,328 KB
testcase_03 AC 33 ms
19,456 KB
testcase_04 AC 33 ms
19,456 KB
testcase_05 AC 39 ms
20,224 KB
testcase_06 AC 35 ms
19,840 KB
testcase_07 AC 34 ms
19,584 KB
testcase_08 AC 45 ms
20,480 KB
testcase_09 AC 45 ms
20,224 KB
testcase_10 AC 38 ms
19,840 KB
testcase_11 AC 40 ms
19,840 KB
testcase_12 AC 32 ms
19,328 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 38 ms
19,968 KB
testcase_24 AC 38 ms
19,968 KB
testcase_25 AC 31 ms
19,328 KB
testcase_26 AC 40 ms
20,096 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.Linq;

namespace No17{
    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 E = sr.Next<int>(M, 3).Select(a => new{A = a[0], B = a[1], C = a[2]}).ToArray();

            var dj = new Dijkstra(N);
            foreach(var e in E){
                dj.AddEdge(e.A, e.B, e.C);
                dj.AddEdge(e.B, e.A, e.C);
            }

            var dist = new int[N][];
            for(var i = 0; i < N; i++){
                dist[i] = dj.Solve(i);
            }

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

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

    public class Dijkstra{
        public class Edge{
            public int To;
            public int Cost;
        }

        public class Node : IComparable<Node>{
            public int Id;
            public int Cost;

            public int CompareTo(Node node){
                return Math.Sign(Cost - node.Cost);
            }
        }

        private readonly List<Edge>[] _adj;

        public Dijkstra(int v){
            _adj = new List<Edge>[v];
            for(var i = 0; i < v; i++) _adj[i] = new List<Edge>();
        }

        public void AddEdge(int from, int to, int cost){
            _adj[from].Add(new Edge{To = to, Cost = cost});
        }

        public int[] Solve(int start){
            var v = _adj.Length;
            var res = new int[v];

            for(var i = 0; i < v; i++) res[i] = int.MaxValue;
            res[start] = 0;

            var pq = new PriorityQueue<Node>();
            pq.Enqueue(new Node{Id = start, Cost = 0});

            while(pq.Count > 0){
                var stat = pq.Dequeue();
                var from = stat.Id;

                foreach(var edge in _adj[from]){
                    var to = edge.To;
                    var cost = stat.Cost + edge.Cost;

                    if(cost >= res[to]) continue;
                    res[to] = cost;
                    pq.Enqueue(new Node{Id = to, Cost = cost});
                }
            }
            return res;
        }
    }

    public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>{
        private readonly List<T> _list = new List<T>();
        public int Count => _list.Count;

        IEnumerator<T> IEnumerable<T>.GetEnumerator(){
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator(){
            return _list.GetEnumerator();
        }

        public void Clear(){
            _list.Clear();
        }

        public bool Contains(T value){
            return _list.Contains(value);
        }

        public T Peek(){
            return _list[0];
        }

        public void Enqueue(T value){
            _list.Add(value);
            PushHeap();
        }

        public T Dequeue(){
            var ret = _list[0];
            _list[0] = _list[Count - 1];
            _list.RemoveAt(Count - 1);
            PopHeap();
            return ret;
        }

        private void PushHeap(){
            var i = Count - 1;
            while(i != 0){
                var p = (i - 1) / 2;
                if(_list[i].CompareTo(_list[p]) > 0) return;

                SwapIndex(i, i = p);
            }
        }

        private void PopHeap(){
            var i = 0;
            while(true){
                var l = 2 * i + 1;
                var r = l + 1;

                var maxi = i;
                if(l < Count && _list[maxi].CompareTo(_list[l]) > 0) maxi = l;
                if(r < Count && _list[maxi].CompareTo(_list[r]) > 0) maxi = r;
                if(maxi == i) return;

                SwapIndex(i, i = maxi);
            }
        }

        private void SwapIndex(int left, int right){
            var tmp = _list[left];
            _list[left] = _list[right];
            _list[right] = tmp;
        }
    }

    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