結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tassi_2012tassi_2012
提出日時 2015-03-02 00:49:44
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,301 bytes
コンパイル時間 3,168 ms
コンパイル使用メモリ 111,720 KB
実行使用メモリ 45,472 KB
最終ジャッジ日時 2024-06-24 00:52:41
合計ジャッジ時間 7,583 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other TLE * 1 -- * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
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