結果

問題 No.160 最短経路のうち辞書順最小
ユーザー AreTrashAreTrash
提出日時 2016-09-01 04:34:47
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 5,959 bytes
コンパイル時間 1,090 ms
コンパイル使用メモリ 113,036 KB
実行使用メモリ 30,948 KB
最終ジャッジ日時 2024-11-14 14:04:32
合計ジャッジ時間 3,077 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
26,348 KB
testcase_01 AC 29 ms
24,240 KB
testcase_02 AC 29 ms
26,408 KB
testcase_03 AC 30 ms
26,660 KB
testcase_04 AC 35 ms
26,280 KB
testcase_05 AC 42 ms
26,316 KB
testcase_06 AC 46 ms
29,032 KB
testcase_07 AC 32 ms
24,568 KB
testcase_08 AC 32 ms
24,316 KB
testcase_09 AC 31 ms
24,628 KB
testcase_10 AC 33 ms
24,236 KB
testcase_11 AC 31 ms
24,492 KB
testcase_12 AC 32 ms
24,496 KB
testcase_13 AC 32 ms
24,572 KB
testcase_14 AC 34 ms
26,352 KB
testcase_15 AC 33 ms
24,568 KB
testcase_16 AC 32 ms
24,372 KB
testcase_17 AC 32 ms
24,624 KB
testcase_18 AC 31 ms
24,488 KB
testcase_19 AC 32 ms
24,624 KB
testcase_20 AC 31 ms
24,496 KB
testcase_21 AC 31 ms
24,368 KB
testcase_22 AC 30 ms
24,496 KB
testcase_23 AC 33 ms
24,312 KB
testcase_24 AC 32 ms
24,496 KB
testcase_25 AC 32 ms
24,496 KB
testcase_26 AC 32 ms
26,364 KB
testcase_27 AC 30 ms
22,324 KB
testcase_28 AC 53 ms
30,948 KB
testcase_29 AC 29 ms
24,440 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;

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

            var dj = new Dijkstra(N);
            for(var i = 0; i < M; i++){
                var A = sr.Next<int>();
                var B = sr.Next<int>();
                var C = sr.Next<int>();

                dj.AddEdge(A, B, C);
                dj.AddEdge(B, A, C);
            }

            var dist = dj.Solve(G);
            var res = new List<int>();

            var now = S;
            while(now != G){
                res.Add(now);

                var next = int.MaxValue;
                foreach(var edge in dj.Adj[now]){
                    if(dist[now] == dist[edge.To] + edge.Cost){
                        next = Math.Min(next, edge.To);
                    }
                }
                now = next;
            }

            res.Add(G);
            Console.WriteLine(string.Join(" ", res));
            //---------------------------------
        }
    }

    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);
        }
    }

    public class Dijkstra{
        public 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 used = new bool[v];
            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;
                //used[from] = true;

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

                    if(/*used[to] ||*/ 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;

        public StreamReader(System.IO.Stream st){
            _sr = new System.IO.StreamReader(st);
        }

        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