結果

問題 No.17 2つの地点に泊まりたい
ユーザー femtofemto
提出日時 2015-03-31 10:38:49
言語 C#(csc)
(csc 3.9.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 8,910 bytes
コンパイル時間 1,102 ms
コンパイル使用メモリ 118,248 KB
実行使用メモリ 37,088 KB
最終ジャッジ日時 2024-07-03 23:02:32
合計ジャッジ時間 5,260 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
23,608 KB
testcase_01 AC 33 ms
27,368 KB
testcase_02 AC 35 ms
27,428 KB
testcase_03 AC 89 ms
25,980 KB
testcase_04 AC 53 ms
27,432 KB
testcase_05 AC 204 ms
28,632 KB
testcase_06 AC 137 ms
30,128 KB
testcase_07 AC 96 ms
26,096 KB
testcase_08 AC 381 ms
37,088 KB
testcase_09 AC 385 ms
28,800 KB
testcase_10 AC 189 ms
23,680 KB
testcase_11 AC 298 ms
28,160 KB
testcase_12 AC 31 ms
19,200 KB
testcase_13 AC 32 ms
19,180 KB
testcase_14 AC 32 ms
19,072 KB
testcase_15 AC 32 ms
19,048 KB
testcase_16 AC 31 ms
19,048 KB
testcase_17 AC 36 ms
19,328 KB
testcase_18 AC 75 ms
20,864 KB
testcase_19 AC 50 ms
20,224 KB
testcase_20 AC 34 ms
19,260 KB
testcase_21 AC 33 ms
19,180 KB
testcase_22 AC 89 ms
21,376 KB
testcase_23 WA -
testcase_24 AC 222 ms
24,448 KB
testcase_25 AC 34 ms
19,156 KB
testcase_26 AC 325 ms
28,800 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.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using Enu = System.Linq.Enumerable;

class Solution {
    int N, M;
    int[] S;
    void calc() {
        N = ri();
        S = new int[N];
        for (int i = 0; i < N; i++) {
            S[i] = ri();
        }
        var dijkstra = new Algorithm.Dijkstra(N);
        dijkstra.S = S;

        M = ri();
        for (int i = 0; i < M; i++) {
            var r = ria(3);
            dijkstra.addUnDirEdge(r[0], r[1], r[2]);
        }

        dijkstra.calcShortestPath(0);

        int ans = dijkstra.INF;

        for (int i = 0; i <= 2000; i++) {
            ans = Math.Min(ans, dijkstra.d[N - 1, i, 2] + i);
        }

        Console.WriteLine(ans);
    }

    static void Main(string[] args) {
        new Solution().calc();
    }

    #region
    static int ri() { return int.Parse(Console.ReadLine()); }
    static int[] ria(int n) {
        if (n <= 0) { Console.ReadLine(); return new int[0]; }
        else return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
    }
    static void rio(out int p1) { p1 = ri(); }
    static void rio(out int p1, out int p2) { var r = ria(2); p1 = r[0]; p2 = r[1]; }
    static void rio(out int p1, out int p2, out int p3) { var r = ria(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; }
    static void rio(out int p1, out int p2, out int p3, out int p4) { var r = ria(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; }
    static void rio(out int p1, out int p2, out int p3, out int p4, out int p5) { var r = ria(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; }
    static long rl() { return long.Parse(Console.ReadLine()); }
    static long[] rla(int n) {
        if (n <= 0) { Console.ReadLine(); return new long[0]; }
        else return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
    }
    static void rlo(out long p1) { p1 = rl(); }
    static void rlo(out long p1, out long p2) { var r = rla(2); p1 = r[0]; p2 = r[1]; }
    static void rlo(out long p1, out long p2, out long p3) { var r = rla(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; }
    static void rlo(out long p1, out long p2, out long p3, out long p4) { var r = rla(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; }
    static void rlo(out long p1, out long p2, out long p3, out long p4, out long p5) { var r = rla(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; }
    static double rd() { return double.Parse(Console.ReadLine()); }
    static double[] rda(int n) {
        if (n <= 0) { Console.ReadLine(); return new double[0]; }
        else return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray();
    }
    static void rdo(out double p1) { p1 = rd(); }
    static void rdo(out double p1, out double p2) { var r = rda(2); p1 = r[0]; p2 = r[1]; }
    static void rdo(out double p1, out double p2, out double p3) { var r = rda(3); p1 = r[0]; p2 = r[1]; p3 = r[2]; }
    static void rdo(out double p1, out double p2, out double p3, out double p4) { var r = rda(4); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; }
    static void rdo(out double p1, out double p2, out double p3, out double p4, out double p5) { var r = rda(5); p1 = r[0]; p2 = r[1]; p3 = r[2]; p4 = r[3]; p5 = r[4]; }
    static void swap<T>(ref T x, ref T y) { T temp = x; x = y; y = temp; }
    static void wa1<T>(T[] a) { Debug.WriteLine(string.Join(" ", a)); }
    static void wa2<T>(T[][] a) {
        foreach (var row in a) {
            Debug.WriteLine(String.Join(" ", row));
        }
    }

    #endregion
}

static class Extention {
    public static T[][] ToJagArray<T>(this T[,] a) {
        int n = a.GetLength(0), m = a.GetLength(1);
        var ret = new T[n][];
        for (int i = 0; i < n; i++) {
            ret[i] = new T[m];
            for (int j = 0; j < m; j++) {
                ret[i][j] = a[i, j];
            }
        }
        return ret;
    }
}

namespace Algorithm {
    using T = System.Int32;
    // require PriorityQueue
    public class Dijkstra {
        public T INF = (T)1e9;
        [DebuggerDisplay("from : {from}, to : {to}, cost : {cost}")]
        private class edge {
            public int from, to;
            public T cost;
            public edge(int from, int to, T cost) {
                this.from = from; this.to = to; this.cost = cost;
            }
        }
        [DebuggerDisplay("v : {v}, cost : {cost}, time : {time}, wait : {waitcount}")]
        private class state {
            public int v, waitcount, first;
            public T cost, time;
            public state(int v, T cost, T time, int waitcount, int first) {
                this.v = v; this.cost = cost; this.time = time;
                this.waitcount = waitcount; this.first = first;
            }
        }
        private int vnum;
        private List<edge>[] edgelists;
        public T[, ,] d;
        private PriorityQueue<state> que;
        public int[] S;
        public Dijkstra(int vnum) {
            this.vnum = vnum;
            edgelists = new List<edge>[vnum + 1];
            for (int i = 0; i < edgelists.Length; i++)
                edgelists[i] = new List<edge>();
            d = new T[vnum + 1, 2001, 3];
        }
        public void addDirEdge(int v1, int v2, T cost) {
            edgelists[v1].Add(new edge(v1, v2, cost));
        }
        public void addUnDirEdge(int v1, int v2, T cost) {
            edgelists[v1].Add(new edge(v1, v2, cost));
            edgelists[v2].Add(new edge(v2, v1, cost));
        }
        public void calcShortestPath(int s) {
            for (int i = 0; i <= vnum; i++) {
                for (int j = 0; j <= 2000; j++) {
                    for (int k = 0; k <= 2; k++) {
                        d[i, j, k] = INF;
                    }
                }
            }
            que = new PriorityQueue<state>((s1, s2) => s2.cost.CompareTo(s1.cost));
            d[s, 0, 0] = 0;
            que.Push(new state(s, 0, 0, 0, 0));

            while (que.Count() != 0) {
                var st = que.Pop();
                int v = st.v;
                if (d[v, st.time, st.waitcount] < st.cost) continue;
                foreach (var e in edgelists[v]) {
                    if (d[e.to, st.time, st.waitcount] > d[v, st.time, st.waitcount] + e.cost) {
                        d[e.to, st.time, st.waitcount] = d[v, st.time, st.waitcount] + e.cost;
                        que.Push(new state(e.to, d[e.to, st.time, st.waitcount], st.time, st.waitcount, st.first));
                    }
                    if (st.v != 0 && st.v != vnum - 1 && st.waitcount <= 1 && st.v != st.first) {
                        if (d[e.to, st.time + S[st.v], st.waitcount + 1] > d[v, st.time, st.waitcount] + e.cost) {
                            d[e.to, st.time + S[st.v], st.waitcount + 1] = d[v, st.time, st.waitcount] + e.cost;
                            que.Push(new state(e.to, d[e.to, st.time + S[st.v], st.waitcount + 1],
                                st.time + S[st.v], st.waitcount + 1, st.v));
                        }
                    }
                }
            }
        }
    }

    public class PriorityQueue<T> {
        private List<T> buffer;
        private Comparison<T> comp;
        // 昇順
        // (e1, e2) => e2.CompareTo(e1)  
        // 降順
        // (e1, e2) => e1.CompareTo(e2)
        public PriorityQueue(Comparison<T> comp) {
            buffer = new List<T>();
            this.comp = comp;
        }
        public PriorityQueue(Comparison<T> comp, IEnumerable<T> data)
            : this(comp) {
            foreach (T e in data) Push(e);
        }

        public void Push(T elem) { PushHeap(this.buffer, elem); }
        public T Pop() { return PopHeap(this.buffer); }
        public int Count() { return buffer.Count; }

        public T Top() { return buffer[0]; }
        private void PushHeap(List<T> array, T elem) {
            int n = array.Count;
            array.Add(elem);

            while (n != 0) {
                int i = (n - 1) / 2;
                // 親と値を入れ替え
                if (comp(array[n], array[i]) > 0) {
                    T tmp = array[n]; array[n] = array[i]; array[i] = tmp;
                }
                n = i;
            }
        }

        private T PopHeap(List<T> array) {
            int n = array.Count - 1;
            T returnelem = array[0];
            array[0] = array[n];
            array.RemoveAt(n);

            for (int i = 0, j; (j = 2 * i + 1) < n; ) {
                // 値の大きい方の子を選ぶ
                if ((j != n - 1) && (comp(array[j], array[j + 1]) < 0))
                    j++;
                // 子と値を入れ替え
                if (comp(array[i], array[j]) < 0) {
                    T tmp = array[j]; array[j] = array[i]; array[i] = tmp;
                }
                i = j;
            }
            return returnelem;
        }
    }
}
0