Submission #587280


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace G
{
    class Program
    {
        static void Main(string[] args)
        {

            int N, M;
            var items = Console.ReadLine().Split(' ');
            N = int.Parse(items[0]);
            M = int.Parse(items[1]);

            var lines = new string[M];
            for (int m = 0; m < M; m++)
            {
                lines[m] = Console.ReadLine();
            }
            //int N = 6, M = 7;
            //var lines = new string[] {
            //    "1 2 1 6",
            //    "1 3 7 4",
            //    "2 3 3 4",
            //    "2 5 6 6",
            //    "3 4 4 6",
            //    "3 5 1 3",
            //    "5 6 2 4"

            //};



            var ways =new Way   [M];
            for (int m = 0; m < M; m++)
            {
                ways[m] = new Way(lines[m]);
            }
            var wayDic = new Dictionary<int, List<Way>>();
            for (int n = 0; n < N; n++)
            {
                wayDic[n] = new List<Way>();
            }
            foreach (var way in ways)
            {
                wayDic[way.a].Add(way);
                wayDic[way.b].Add(way);
            }


            Dictionary<int, long>[] minTimes = new Dictionary<int, long>[N];
            HashSet<int>[] changed = new  HashSet<int> [N];
            for (int i = 0; i < N; i++)
            {
                changed[i] = new  HashSet<int>();   
                minTimes[i] = new Dictionary<int, long>();

                foreach (var way in wayDic[i])
                {
                    minTimes[i][way.c] = long.MaxValue/2;
                }
            }


            minTimes[0][1] = 0;
            changed[0].Add(1);
            bool loop = true;
            while (loop)
            {
                loop = false;


                for (int n = 0; n < N; n++)
                {
                    var changedColors = changed[n];
                    changed[n] = new  HashSet<int>();
                    foreach (var color in changedColors)
                    {
                        foreach (var way in wayDic[n])
                        {
                            int n2 = way.a == n ? way.b : way.a;
                            long nextTime = minTimes[n][color] + Math.Abs(color - way.c) + way.t;
                            if (minTimes[n2][way.c] > nextTime)
                            {
                                minTimes[n2][way.c] = nextTime;
                                changed[n2].Add(way.c);
                                loop = true;
                            }
                            //Console.WriteLine("{0} {1}", n, n2);
                        }
                    }
                }
            }

            long  result = minTimes[N - 1].Values.Min();

            Console.WriteLine(result);


        }



        class Way
        {
            public Way(string line)
            {
                var items = line.Split(' ');
                a = int.Parse(items[0])-1;
                b = int.Parse(items[1])-1;
                c = int  .Parse(items[2]);
                t = long .Parse(items[3]);

            }

            public int a;
            public int b;
            public int  c;
            public long  t;
        }

    }
}

Submission Info

Submission Time
Task G - カメレオン
User exaMG
Language C# (Mono 3.2.1.0)
Score 0
Code Size 3476 Byte
Status TLE
Exec Time 4052 ms
Memory 147788 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 15
TLE × 5
Set Name Test Cases
Sample sample-01.txt, sample-02.txt
All test-01.txt, test-02.txt, test-03.txt, test-04.txt, test-05.txt, test-06.txt, test-07.txt, test-08.txt, test-09.txt, test-10.txt, test-11.txt, test-12.txt, test-13.txt, test-14.txt, test-15.txt, test-16.txt, test-17.txt, test-18.txt, test-19.txt, test-20.txt
Case Name Status Exec Time Memory
sample-01.txt AC 169 ms 11752 KiB
sample-02.txt AC 171 ms 11760 KiB
test-01.txt AC 172 ms 11736 KiB
test-02.txt AC 176 ms 12008 KiB
test-03.txt AC 181 ms 13088 KiB
test-04.txt AC 200 ms 14660 KiB
test-05.txt AC 198 ms 14748 KiB
test-06.txt AC 2425 ms 31476 KiB
test-07.txt TLE 4042 ms 43352 KiB
test-08.txt AC 866 ms 52540 KiB
test-09.txt AC 1193 ms 65876 KiB
test-10.txt AC 226 ms 23372 KiB
test-11.txt AC 347 ms 33780 KiB
test-12.txt AC 2014 ms 76736 KiB
test-13.txt TLE 4041 ms 43076 KiB
test-14.txt AC 2555 ms 127716 KiB
test-15.txt AC 819 ms 101844 KiB
test-16.txt TLE 4052 ms 126924 KiB
test-17.txt AC 2666 ms 127884 KiB
test-18.txt TLE 4048 ms 147788 KiB
test-19.txt TLE 4050 ms 117712 KiB
test-20.txt AC 3234 ms 139804 KiB