結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tassi_2012tassi_2012
提出日時 2015-03-02 00:49:44
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,301 bytes
コンパイル時間 4,504 ms
コンパイル使用メモリ 102,860 KB
実行使用メモリ 43,768 KB
最終ジャッジ日時 2023-09-06 06:12:33
合計ジャッジ時間 11,257 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
20,708 KB
testcase_01 AC 60 ms
20,620 KB
testcase_02 AC 78 ms
23,484 KB
testcase_03 WA -
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
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;

class program
{
    public static void Main()
    {
        var str = Console.ReadLine().Split(' ');
        var N = int.Parse(str[0]);
        var M = int.Parse(str[1]);
        var S = int.Parse(str[2]);
        var G = int.Parse(str[3]);

        var a = new int[M];
        var b = new int[M];
        var c = new int[M];

        for (var i = 0; i < M; i++)
        {
            str = Console.ReadLine().Split(' ');
            a[i] = int.Parse(str[0]);
            b[i] = int.Parse(str[1]);
            c[i] = int.Parse(str[2]);
        }

        var dic = new Dictionary<string, int>();

        checkNext(a, b, c, S, G, 0, "", dic, new List<int>());

        var min = 1000000000;
        var bestpath = "";
        foreach (KeyValuePair<string, int> pair in dic)
        {
            // Console.WriteLine("{0} {1}", pair.Key, pair.Value);
            if (pair.Value < min)
            {
                bestpath = pair.Key;
                min = pair.Value;
            }
            else if (pair.Value == min)
            {
                if (bestpath.CompareTo(pair.Key) > 0)
                {
                    bestpath = pair.Key;
                }
            }
        }
        Console.WriteLine(bestpath);
    }

    public static void checkNext(int[] a, int[] b, int[] c, int S, int G, int val, string path, Dictionary<string, int> dic, List<int> passed)
    {
        // Console.WriteLine("{0} {1}:{2}",S, G,path);
        if (S == G)
        {
            dic.Add(path + S.ToString(), val);
            return;
        }
        for (var i = 0; i < a.Length; i++)
        {
            var flag = false;
            foreach (var con in passed)
            {
                if (flag) break;
                if (i == con)
                {
                    flag = true;
                }
            }
            if (flag) continue;

            if (a[i] == S)
            {
                passed.Add(i);
                checkNext(a, b, c, b[i], G, val + c[i], path + S.ToString() + " ", dic, passed);
            }
            else if (b[i] == S)
            {
                passed.Add(i);
                checkNext(a, b, c, a[i], G, val + c[i], path + S.ToString() + " ", dic, passed);
            }
        }
    }
}
0