結果

問題 No.134 走れ!サブロー君
ユーザー eitahoeitaho
提出日時 2015-01-22 23:50:28
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 84 ms / 5,000 ms
コード長 3,730 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 109,932 KB
実行使用メモリ 25,864 KB
最終ジャッジ日時 2023-08-04 19:50:45
合計ジャッジ時間 4,029 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
23,748 KB
testcase_01 AC 62 ms
23,612 KB
testcase_02 AC 63 ms
23,676 KB
testcase_03 AC 68 ms
25,816 KB
testcase_04 AC 64 ms
25,864 KB
testcase_05 AC 67 ms
23,740 KB
testcase_06 AC 68 ms
25,860 KB
testcase_07 AC 71 ms
23,824 KB
testcase_08 AC 84 ms
24,284 KB
testcase_09 AC 80 ms
24,068 KB
testcase_10 AC 63 ms
25,624 KB
testcase_11 AC 64 ms
23,708 KB
testcase_12 AC 61 ms
23,672 KB
testcase_13 AC 63 ms
23,776 KB
testcase_14 AC 62 ms
21,752 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.Text;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using Enu = System.Linq.Enumerable;

class Program
{
    static readonly double INF = 1e100;
    int N;
    int[] X, Y;
    double[] W, Wsum;

    void Solve()
    {
        int SX = reader.Int(), SY = reader.Int();
        N = reader.Int();
        X = new int[N];
        Y = new int[N];
        W = new double[N];
        for (int i = 0; i < N; i++)
        {
            X[i] = reader.Int(); Y[i] = reader.Int(); W[i] = reader.Double();
        }
        Wsum = new double[1 << N];
        for (int seen = 0; seen < (1 << N); seen++)
            for (int i = 0; i < N; i++)
                if ((seen >> i & 1) == 0)
                    Wsum[seen] += W[i];

        var dp = new double[1 << N, N];
        Fill(dp, INF);
        for (int i = 0; i < N; i++)
            dp[1 << i, i] = Cost(SX, SY, i, Wsum[0], W[i]);
        for (int seen = 0; seen < (1 << N); seen++)
            for (int last = 0; last < N; last++)
                if ((seen >> last & 1) == 1)
                    for (int next = 0; next < N; next++)
                        if ((seen >> next & 1) == 0)
                        {
                            double cost = dp[seen, last] + Cost(X[last], Y[last], next, Wsum[seen], W[next]);
                            CheckMin(ref dp[seen | (1 << next), next], cost);
                        }

        double ans = Enu.Range(0, N).Min(i => dp[(1 << N) - 1, i] + Cost(SX, SY, i, Wsum[(1 << N) - 1], 0));
        Console.WriteLine(ans);
        Console.ReadLine();
    }

    double Cost(int sx, int sy, int next, double wsum, double w)
    {
        double d = Math.Abs(sx - X[next]) + Math.Abs(sy - Y[next]);
        return d * (wsum + 100) / 120.0 + w;
    }

    void CheckMin(ref double a, double b) { if (b < a) a = b; }

    void Fill<T>(T[,] a, T val)
    {
        for (int i = 0; i < a.GetLength(0); i++)
            for (int j = 0; j < a.GetLength(1); j++)
                a[i, j] = val;
    }

    static void Main() { new Program().Solve(); }
    Reader reader = new Reader(Console.In);
    class Reader
    {
        private readonly TextReader reader;
        private readonly char[] separator = { ' ' };
        private readonly StringSplitOptions removeOp = StringSplitOptions.RemoveEmptyEntries;
        private string[] A = new string[0];
        private int i;

        public Reader(TextReader r) { reader = r; }
        public bool HasNext() { return Enqueue(); }
        public string String() { return Dequeue(); }
        public int Int() { return int.Parse(Dequeue()); }
        public long Long() { return long.Parse(Dequeue()); }
        public double Double() { return double.Parse(Dequeue()); }
        public int[] IntLine() { var s = Line(); return s == "" ? new int[0] : Array.ConvertAll(Split(s), int.Parse); }
        public int[] IntArray(int N) { return Enumerable.Range(0, N).Select(i => Int()).ToArray(); }
        public int[][] IntGrid(int H) { return Enumerable.Range(0, H).Select(i => IntLine()).ToArray(); }
        public string[] StringArray(int N) { return Enumerable.Range(0, N).Select(i => Line()).ToArray(); }
        public string Line() { return reader.ReadLine().Trim(); }
        private string[] Split(string s) { return s.Split(separator, removeOp); }
        private bool Enqueue()
        {
            if (i < A.Length) return true;
            string line = reader.ReadLine();
            if (line == null) return false;
            if (line == "") return Enqueue();
            A = Split(line);
            i = 0;
            return true;
        }
        private string Dequeue() { Enqueue(); return A[i++]; }
    }
}
0