Submission #13288051


Source Code Expand

Copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Library;
using static Library.Input;

namespace AtCoderTemplate
{
    class ProgramE
    {
        private class Edge {
            public int From {get;set;}
            public int To {get;set;}
            public long Cost {get;set;}
        }
        static void Main()
        {
            int n = NextInt, m = NextInt, p = NextInt;
            var edges = TakeLine(m).Select(l => new Edge{From = (int)l[0] - 1, To = (int)l[1] - 1, Cost = l[2] - p}).ToArray();
            var distance = Enumerable.Repeat(long.MinValue >> 2, n).ToArray();
            distance[0] = 0;
            var negatives = Enumerable.Repeat(false, n).ToArray();

            for (int i = 0; i <= 2 * n; i++)
            {
                foreach (var edge in edges)
                {
                    if (distance[edge.From] != long.MinValue >> 2 && distance[edge.To] < distance[edge.From] + edge.Cost)
                    {
                        if (i >= n - 1 && edge.To == n - 1)
                        {
                            System.Console.WriteLine(-1);
                            return;
                        }
                        else if (i >= n - 1)
                        {
                            distance[edge.To] = long.MaxValue >> 2;
                        }
                        else
                        {
                            distance[edge.To] = distance[edge.From] + edge.Cost;
                        }
                    }
                }
            }

            System.Console.WriteLine(Math.Max(0, distance[n - 1]));
        }
    }
}

namespace Library
{
    class Input
    {
        static IEnumerator<string> enumerator = new string[] { }.AsEnumerable().GetEnumerator();

        public static string Line => Console.ReadLine();

        public static string[] StrArr => Line.Split(' ');

        public static int NextInt => int.Parse(NextWord());

        public static long NextLong => long.Parse(NextWord());

        public static List<int> IntList => StrArr.Select(int.Parse).ToList();

        public static List<long> LongList => StrArr.Select(long.Parse).ToList();

        public static IEnumerable<long[]> TakeLine(int N)
        {
            return Enumerable.Repeat(0, N).Select(_ => Console.ReadLine().Split(' ').Select(long.Parse).ToArray());
        }

        public static string NextWord()
        {
            while (!enumerator.MoveNext())
            {
                enumerator = StrArr.AsEnumerable().GetEnumerator();
            }
            return enumerator.Current;
        }
    }
}

Submission Info

Submission Time
Task E - Coins Respawn
User asurato
Language C# (Mono 4.6.2.0)
Score 500
Code Size 2762 Byte
Status AC
Exec Time 272 ms
Memory 15644 KB

Judge Result

Set Name Sample All after_contest
Score / Max Score 0 / 0 500 / 500 0 / 0
Status
AC × 3
AC × 56
AC × 3
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56
after_contest after_contest_57, after_contest_58, after_contest_59
Case Name Status Exec Time Memory
a01 AC 30 ms 13524 KB
a02 AC 29 ms 9428 KB
a03 AC 29 ms 9428 KB
after_contest_57 AC 86 ms 9500 KB
after_contest_58 AC 87 ms 11548 KB
after_contest_59 AC 87 ms 11548 KB
b04 AC 29 ms 9428 KB
b05 AC 29 ms 11476 KB
b06 AC 29 ms 9428 KB
b07 AC 29 ms 9428 KB
b08 AC 29 ms 11476 KB
b09 AC 88 ms 9500 KB
b10 AC 141 ms 9500 KB
b11 AC 115 ms 11548 KB
b12 AC 118 ms 9500 KB
b13 AC 119 ms 13596 KB
b14 AC 146 ms 11548 KB
b15 AC 37 ms 15644 KB
b16 AC 37 ms 13596 KB
b17 AC 37 ms 11548 KB
b18 AC 141 ms 11548 KB
b19 AC 142 ms 11548 KB
b20 AC 88 ms 11548 KB
b21 AC 89 ms 13596 KB
b22 AC 78 ms 9500 KB
b23 AC 157 ms 13596 KB
b24 AC 77 ms 11548 KB
b25 AC 29 ms 11508 KB
b26 AC 261 ms 13536 KB
b27 AC 260 ms 13536 KB
b28 AC 261 ms 13536 KB
b29 AC 150 ms 13536 KB
b30 AC 150 ms 15584 KB
b31 AC 148 ms 11488 KB
b32 AC 261 ms 13536 KB
b33 AC 266 ms 13536 KB
b34 AC 262 ms 11488 KB
b35 AC 267 ms 13536 KB
b36 AC 264 ms 13536 KB
b37 AC 269 ms 13536 KB
b38 AC 265 ms 13536 KB
b39 AC 265 ms 11488 KB
b40 AC 265 ms 13536 KB
b41 AC 267 ms 15584 KB
b42 AC 266 ms 13536 KB
b43 AC 215 ms 13536 KB
b44 AC 212 ms 13536 KB
b45 AC 229 ms 13536 KB
b46 AC 204 ms 11488 KB
b47 AC 132 ms 13536 KB
b48 AC 136 ms 11548 KB
b49 AC 145 ms 11548 KB
b50 AC 104 ms 11548 KB
b51 AC 88 ms 11548 KB
b52 AC 103 ms 9500 KB
b53 AC 115 ms 13596 KB
b54 AC 234 ms 11488 KB
b55 AC 261 ms 11488 KB
b56 AC 272 ms 11488 KB