結果

問題 No.1 道のショートカット
ユーザー mbanmban
提出日時 2017-01-31 23:41:26
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 76 ms / 5,000 ms
コード長 4,273 bytes
コンパイル時間 3,449 ms
コンパイル使用メモリ 106,364 KB
実行使用メモリ 24,124 KB
最終ジャッジ日時 2023-09-27 22:03:52
合計ジャッジ時間 8,180 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
21,720 KB
testcase_01 AC 64 ms
19,608 KB
testcase_02 AC 66 ms
21,832 KB
testcase_03 AC 66 ms
23,788 KB
testcase_04 AC 65 ms
21,764 KB
testcase_05 AC 67 ms
23,780 KB
testcase_06 AC 65 ms
21,656 KB
testcase_07 AC 65 ms
21,764 KB
testcase_08 AC 75 ms
24,124 KB
testcase_09 AC 75 ms
21,768 KB
testcase_10 AC 74 ms
23,896 KB
testcase_11 AC 75 ms
22,012 KB
testcase_12 AC 76 ms
22,200 KB
testcase_13 AC 70 ms
22,168 KB
testcase_14 AC 60 ms
21,652 KB
testcase_15 AC 54 ms
21,636 KB
testcase_16 AC 61 ms
21,840 KB
testcase_17 AC 56 ms
21,572 KB
testcase_18 AC 56 ms
21,632 KB
testcase_19 AC 56 ms
21,732 KB
testcase_20 AC 61 ms
23,948 KB
testcase_21 AC 71 ms
21,892 KB
testcase_22 AC 63 ms
21,792 KB
testcase_23 AC 69 ms
22,108 KB
testcase_24 AC 71 ms
21,988 KB
testcase_25 AC 69 ms
23,876 KB
testcase_26 AC 63 ms
22,004 KB
testcase_27 AC 64 ms
21,992 KB
testcase_28 AC 54 ms
21,640 KB
testcase_29 AC 62 ms
22,040 KB
testcase_30 AC 61 ms
21,976 KB
testcase_31 AC 64 ms
21,888 KB
testcase_32 AC 62 ms
19,952 KB
testcase_33 AC 61 ms
21,768 KB
testcase_34 AC 65 ms
22,056 KB
testcase_35 AC 60 ms
23,964 KB
testcase_36 AC 63 ms
21,840 KB
testcase_37 AC 61 ms
21,776 KB
testcase_38 AC 55 ms
21,664 KB
testcase_39 AC 61 ms
21,796 KB
testcase_40 AC 61 ms
21,856 KB
testcase_41 AC 62 ms
23,864 KB
testcase_42 AC 56 ms
19,676 KB
testcase_43 AC 56 ms
21,780 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;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        Magatro alice = new Magatro();
        alice.Scan();
        alice.Solve();
    }
}

public class Scanner
{
    private StreamReader Sr;
    private string[] S;
    private int Index;
    private const char Separator = ' ';
    public Scanner()
    {
        Index = 0;
        S = new string[0];
        Sr = new StreamReader(Console.OpenStandardInput());
    }
    private string[] Line()
    {
        return Sr.ReadLine().Split(Separator);
    }
    public string Next()
    {
        string result;
        if (Index >= S.Length)
        {
            S = Line();
            Index = 0;
        }
        result = S[Index];
        Index++;
        return result;
    }
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
    public decimal NextDecimal()
    {
        return decimal.Parse(Next());
    }
    public string[] StringArray(int index = 0)
    {
        Next();
        Index = S.Length;
        return S.Skip(index).ToArray();
    }
    public int[] IntArray(int index = 0)
    {
        return StringArray(index).Select(int.Parse).ToArray();
    }
    public long[] LongArray(int index = 0)
    {
        return StringArray(index).Select(long.Parse).ToArray();
    }
    public bool EndOfStream
    {
        get { return Sr.EndOfStream; }
    }
}

public class Magatro
{
    private int N, C, V;
    private int[] S, T, Y, M;

    int Infinity = int.MaxValue;

    //iの町からGraph[i].Tの町までYのコストで時間M
    private List<Road>[] Graph;

    //[現在の町の位置,かかるコスト]
    private int[,] Time;

    public void Scan()
    {
        Scanner sc = new Scanner();
        N = sc.NextInt();
        C = sc.NextInt();
        V = sc.NextInt();
        S = sc.IntArray();
        T = sc.IntArray();
        Y = sc.IntArray();
        M = sc.IntArray();
    }

    public void Solve()
    {
        InitGraph();
        InitTime();
        Calc();
        Write();
    }

    //S,T,Y,MをGraphに入れる
    private void InitGraph()
    {
        Graph = new List<Road>[N];

        for (int i = 0; i < N; i++)
        {
            Graph[i] = new List<Road>();
        }

        for (int i = 0; i < V; i++)
        {
            Graph[S[i]-1].Add(new Road(T[i] - 1, Y[i], M[i]));
        }
    }

    //Timeの値をInfinityにして初期化
    private void InitTime()
    {
        Time = new int[N, C + 1];

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j <= C; j++)
            {
                Time[i, j] = Infinity;
            }
        }
    }

    private void Calc()
    {
        Time[0, 0] = 0;

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j <= C; j++)
            {
                if (Time[i, j] != Infinity)
                {
                    foreach (Road r in Graph[i])
                    {
                        //コストがC以下なら
                        if (j + r.Y <= C)
                        {
                            //Timeを更新
                            Time[r.T, j + r.Y] = Math.Min(Time[r.T, j + r.Y], Time[i, j] + r.M);
                        }
                    }
                }
            }
        }
    }

    private void Write()
    {
        int anser = int.MaxValue;
        bool flag = false;

        for(int i = 0; i <= C; i++)
        {
            if (Time[N - 1, i] != Infinity)
            {
                //flagを立ててanserを更新
                anser = Math.Min(anser, Time[N - 1, i]);
                flag = true;
            }
        }

        if (flag)
        {
            Console.WriteLine(anser);
        }
        else
        {
            Console.WriteLine(-1);
        }
    }
}

struct Road
{
    public int T, Y, M;

    public Road(int t,int y,int m)
    {
        T = t;
        Y = y;
        M = m;
    }
}
0