結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 14番14番
提出日時 2016-04-27 00:53:30
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 4,546 bytes
コンパイル時間 1,133 ms
コンパイル使用メモリ 114,272 KB
実行使用メモリ 230,612 KB
最終ジャッジ日時 2024-04-15 04:34:52
合計ジャッジ時間 8,046 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
26,504 KB
testcase_01 AC 28 ms
24,448 KB
testcase_02 AC 30 ms
24,324 KB
testcase_03 AC 31 ms
24,212 KB
testcase_04 AC 49 ms
30,780 KB
testcase_05 AC 72 ms
30,676 KB
testcase_06 AC 89 ms
29,752 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Generic;
using System.Text;
using System.Linq;
 
class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        
        int[] inpt = Reader.GetInt();
        int ekiCount = inpt[0];
        int roadCount = inpt[1];
        this.StartEki = inpt[2];
        this.GoalEki = inpt[3];
        
        Dictionary<int, Dictionary<int, long>> dic = new Dictionary<int,Dictionary<int, long>>();
        
        for(int i=0; i<roadCount; i++) {
            inpt = Reader.GetInt();
            if(!dic.ContainsKey(inpt[0])) {
                dic.Add(inpt[0], new Dictionary<int, long>());
            }
            dic[inpt[0]].Add(inpt[1], inpt[2]);
            if(!dic.ContainsKey(inpt[1])) {
                dic.Add(inpt[1], new Dictionary<int, long>());
            }
            dic[inpt[1]].Add(inpt[0], inpt[2]);
        }
        
        Queue<TaskItem> task = new Queue<TaskItem>();
        TaskItem firstTask = new  TaskItem(StartEki);
        task.Enqueue(firstTask);
        
        Keiro[] step = new Keiro[ekiCount + 1];
        step[StartEki] = firstTask.Route;
        while (task.Count > 0)
        {
            TaskItem item = task.Dequeue();
            
            foreach (int key in dic[item.Pos].Keys)
            {
                Keiro kei = item.Route.Duplicate();
                kei.Add(key);
                kei.AddCost(dic[item.Pos][key]);
                if(step[key] == null || step[key].Cost > kei.Cost) {
                    step[key] = kei;
                    TaskItem tsk = new TaskItem(key, kei);
                    task.Enqueue(tsk);
                } else if(step[key].Cost == kei.Cost && kei.CompareTo(step[key]) < 0) {
                    step[key] = kei;
                    TaskItem tsk = new TaskItem(key, kei);
                    task.Enqueue(tsk);
                }
            }
        }
        Console.WriteLine(string.Join(" ", step[GoalEki]));
    }
    
    private int StartEki;
    private int GoalEki;
    
    public class TaskItem 
    {
        public Keiro Route;
        public int Pos;
        
        public TaskItem(int pos) {
            this.Route = new Keiro();
            this.Route.Add(pos);
            this.Pos = pos;
        }
        public TaskItem(int pos, Keiro kei) {
            this.Route = kei;
            this.Pos = pos;
        }
    }

    public class Keiro : List<int>, IComparable<List<int>> 
    {
        public long Cost {
            get {
                return costValue;
            }
        }
        
        public void AddCost(long addCost) {
            this.costValue+=addCost;
        }
        
        private long costValue = 0;
        public int CompareTo(List<int> other)
        {
            for(int i=0; i<Math.Min(this.Count, other.Count); i++) {
                if(this[i] < other[i]) {
                    return -1;
                } else if(this[i] > other[i]) {
                    return 1;
                }
            }
            if(this.Count < other.Count) {
                return -1;
            } else if(this.Count > other.Count) {
                return 1;
            }
            return 0;
        }
        
        public Keiro Duplicate() {
            Keiro newIns = new Keiro();
            newIns.AddRange(this);
            newIns.AddCost(this.costValue);
            return newIns;
        }

    }





    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"




 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0