結果
問題 |
No.1 道のショートカット
|
ユーザー |
![]() |
提出日時 | 2017-01-31 23:41:26 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 40 ms / 5,000 ms |
コード長 | 4,273 bytes |
コンパイル時間 | 1,621 ms |
コンパイル使用メモリ | 113,232 KB |
実行使用メモリ | 20,096 KB |
最終ジャッジ日時 | 2024-07-20 16:28:46 |
合計ジャッジ時間 | 3,889 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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; } }