結果

問題 No.807 umg tours
ユーザー keymoonkeymoon
提出日時 2019-03-22 23:06:49
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 5,446 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 115,852 KB
実行使用メモリ 79,804 KB
最終ジャッジ日時 2023-10-19 10:20:38
合計ジャッジ時間 23,649 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
25,672 KB
testcase_01 AC 38 ms
25,672 KB
testcase_02 AC 39 ms
25,672 KB
testcase_03 AC 39 ms
25,672 KB
testcase_04 AC 38 ms
25,672 KB
testcase_05 AC 38 ms
25,672 KB
testcase_06 AC 40 ms
25,672 KB
testcase_07 AC 39 ms
25,672 KB
testcase_08 AC 38 ms
25,664 KB
testcase_09 AC 37 ms
25,672 KB
testcase_10 AC 36 ms
25,672 KB
testcase_11 AC 1,678 ms
64,184 KB
testcase_12 AC 995 ms
57,796 KB
testcase_13 AC 1,590 ms
68,452 KB
testcase_14 AC 570 ms
48,192 KB
testcase_15 AC 411 ms
41,744 KB
testcase_16 AC 1,798 ms
71,144 KB
testcase_17 AC 2,095 ms
78,796 KB
testcase_18 AC 2,130 ms
78,424 KB
testcase_19 AC 2,087 ms
77,460 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 881 ms
68,264 KB
testcase_25 AC 2,152 ms
79,804 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.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using Debug = System.Diagnostics.Debug;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;


static class P
{
    static void Main()
    {
        //頂点それぞれについてツアー
        //1→i→1
        //単一始点最短路を打って
        //1減る ということを
        var nm = Console.ReadLine().Split().Select(int.Parse).ToList();
        List<Tuple<int, int>>[] neighbour = Enumerable.Repeat(0, nm[0]).Select(_ => new List<Tuple<int, int>>()).ToArray();

        for (int i = 0; i < nm[1]; i++)
        {
            var abc = Console.ReadLine().Split().Select(int.Parse).ToList();
            neighbour[abc[0] - 1].Add(new Tuple<int, int>(abc[1] - 1, abc[2]));
            neighbour[abc[1] - 1].Add(new Tuple<int, int>(abc[0] - 1, abc[2]));
        }

        //0-(n-1) : 通常の点
        //n-(2*n-1) : 既にvipチケ使った点
        int[] distances = Enumerable.Repeat(-1, nm[0] * 2).ToArray();
        //PriorityQueue<Tuple<int, int>> pqueue = new PriorityQueue<Tuple<int, int>>(nm[1]);
        Heap pqueue = new Heap(nm[1] * 8);
        pqueue.Push(new Tuple<int, int>(0, 0));
        while (pqueue.Count > 0)
        {
            var item = pqueue.Pop();
            Debug.WriteLine($"{item.Item2} {item.Item1}");
            if (distances[item.Item2] >= 0) continue;
            distances[item.Item2] = item.Item1;

            if (item.Item2 >= nm[0])
            {
                foreach (var edge in neighbour[item.Item2 - nm[0]])
                {
                    pqueue.Push(new Tuple<int, int>(item.Item1 + edge.Item2, edge.Item1 + nm[0]));
                }
            }
            else
            {
                foreach (var edge in neighbour[item.Item2])
                {
                    pqueue.Push(new Tuple<int, int>(item.Item1 + edge.Item2, edge.Item1));
                    pqueue.Push(new Tuple<int, int>(item.Item1, edge.Item1 + nm[0]));
                }
            }
        }
        Console.WriteLine(string.Join("\n", distances.Zip(distances.Skip(nm[0]), (x, y) => x + Min(x, y))));
    }
}

//  Microsoft Windows Client Platform
//  Copyright (C) Microsoft Corporation. All rights reserved.
internal class PriorityQueue<T> where T : IComparable
{
    private T[] _heap;
    private const int DefaultCapacity = 6;

    public PriorityQueue(int capacity)
    {
        Count = 0;
        _heap = new T[capacity > 0 ? capacity : DefaultCapacity];
    }
    
    public int Count { get; set; }

    public T Top
    {
        get
        {
            Debug.Assert(Count > 0);
            return _heap[0];
        }
    }

    public void Push(T value)
    {
        if (Count == _heap.Length)
        {
            T[] temp = new T[Count * 2];
            for (int i = 0; i < Count; ++i)
            {
                temp[i] = _heap[i];
            }
            _heap = temp;
        }
        
        int index = Count;
        while (index > 0)
        {
            int parentIndex = (index - 1) / 2;
            if (value.CompareTo(_heap[parentIndex]) >= 0) break;
            _heap[index] = _heap[parentIndex];
            index = parentIndex;
        }

        _heap[index] = value;
        Count++;
    }
    
    public T Pop()
    {
        T top = Top;
        Debug.Assert(Count != 0);
        if (Count > 1)
        {
            int parent = 0;
            int leftChild = (parent * 2) + 1;

            while (leftChild < Count)
            {
                int rightChild = leftChild + 1;
                int bestChild = (rightChild < Count && _heap[rightChild].CompareTo(_heap[leftChild]) < 0) ? rightChild : leftChild;
                
                _heap[parent] = _heap[bestChild];

                parent = bestChild;
                leftChild = (parent * 2) + 1;
            }

            _heap[parent] = _heap[Count - 1];
        }
        Count--;
        return top;
    }
}

class Heap
{
    public int Count;
    Tuple<int, int>[] obj;
    public Heap(int maxsize)
    {
        Count = 0;
        obj = new Tuple<int, int>[maxsize];
    }
    public void Push(Tuple<int, int> a)
    {
        int i = Count;
        Count++;
        while (i > 0)
        {
            int p = (i - 1) / 2;
            if (obj[p].Item1 <= a.Item1)
            {
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if (i == 0)
        {
            obj[0] = a;
        }
    }
    public Tuple<int, int> Pop()
    {
        Tuple<int, int> t = obj[0];
        int i = 0;
        Count--;
        while (2 * i + 1 < Count)
        {
            int p = 2 * i + 1;
            if (p + 1 < Count && obj[p + 1].Item1 < obj[p].Item1)
            {
                p++;
            }
            if (obj[p].Item1 < obj[Count].Item1)
            {
                obj[i] = obj[p];
                i = p;
            }
            else
            {
                obj[i] = obj[Count];
                break;
            }
        }
        if (2 * i + 1 >= Count)
        {
            obj[i] = obj[Count];
        }
        return t;
    }
}
0