結果

問題 No.1473 おでぶなおばけさん
ユーザー bluemegane
提出日時 2021-04-11 09:50:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,071 ms / 2,000 ms
コード長 2,442 bytes
コンパイル時間 1,502 ms
コンパイル使用メモリ 107,392 KB
実行使用メモリ 44,396 KB
最終ジャッジ日時 2024-06-27 05:46:32
合計ジャッジ時間 17,417 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using static System.Math;
using System.Collections.Generic;
using System.Linq;
using System;

public class Edge
{
    public int to { get; set; }
    public long d { get; set; }
}

public class P
{
    public int from { get; set; }
    public int step { get; set; }
}

public class Hello
{
    public static int n, step;
    static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        n = int.Parse(line[0]);
        var m = int.Parse(line[1]);
        var aa = new List<Edge>[n];
        for (int i = 0; i < n; i++) aa[i] = new List<Edge>();
        for (int i = 0; i < m; i++)
        {
            line = Console.ReadLine().Trim().Split(' ');
            var s = int.Parse(line[0]) - 1;
            var t = int.Parse(line[1]) - 1;
            var d = int.Parse(line[2]);
            aa[s].Add(new Edge { to = t, d = d });
            aa[t].Add(new Edge { to = s, d = d });
        }
        getAns(aa);
    }
    static void getAns(List<Edge>[] aa)
    {
        var ok = 1;
        var ng = 1000000001;
        while (ng - ok > 1)
        {
            var mid = ng + (ok - ng) / 2;
            if (check(aa, mid)) ok = mid;
            else ng = mid;
        }
        getSTEP(aa, ok);
        Console.WriteLine("{0} {1}", ok, step);
    }
    static void getSTEP(List<Edge>[] aa, int t)
    {
        step = int.MaxValue;
        var visited = new bool[n];
        var q = new Queue<P>();
        q.Enqueue(new P { from = 0, step = 0 });
        visited[0] = true;
        while (q.Count() > 0)
        {
            var w = q.Dequeue();
            if (w.from == n - 1) { step = Min(step, w.step); continue; }
            foreach (var x in aa[w.from])
            {
                if (t <= x.d &&   !visited[x.to]                   )
                {
                    q.Enqueue(new P { from = x.to, step = w.step + 1 });
                    visited[x.to] = true;
                }
            }
        }
    }
    static bool check(List<Edge>[] aa, int t)
    {
        var visited = new bool[n];
        var q = new Queue<int>();
        q.Enqueue(0);
        visited[0] = true;
        while (q.Count() > 0)
        {
            var w = q.Dequeue();
            if (w == n - 1) { return true; }
            foreach (var x in aa[w])
            {
                if (t <= x.d && !visited[x.to]) { q.Enqueue(x.to); visited[x.to] = true; }
            }
        }
        return false;
    }
}
0