結果

問題 No.95 Alice and Graph
ユーザー eitahoeitaho
提出日時 2015-03-15 17:04:27
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 3,643 ms / 5,000 ms
コード長 3,674 bytes
コンパイル時間 1,024 ms
コンパイル使用メモリ 110,592 KB
実行使用メモリ 27,136 KB
最終ジャッジ日時 2024-04-26 21:56:28
合計ジャッジ時間 14,466 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,307 ms
27,136 KB
testcase_01 AC 256 ms
27,008 KB
testcase_02 AC 247 ms
26,752 KB
testcase_03 AC 307 ms
27,136 KB
testcase_04 AC 1,830 ms
26,880 KB
testcase_05 AC 1,492 ms
27,008 KB
testcase_06 AC 1,771 ms
27,008 KB
testcase_07 AC 256 ms
27,136 KB
testcase_08 AC 31 ms
19,328 KB
testcase_09 AC 32 ms
18,944 KB
testcase_10 AC 32 ms
19,072 KB
testcase_11 AC 32 ms
19,456 KB
testcase_12 AC 32 ms
19,200 KB
testcase_13 AC 3,643 ms
27,136 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.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Enu = System.Linq.Enumerable;

class Program
{
    void Solve()
    {
        int N = reader.Int(), M = reader.Int(), K = reader.Int();
        int[][] dist = Enu.Range(0, N).Select(i => Enu.Repeat(K + 1, N).ToArray()).ToArray();
        for (int i = 0; i < N; i++)
            dist[i][i] = 0;
        for (int i = 0; i < M; i++)
        {
            int a = reader.Int() - 1, b = reader.Int() - 1;
            dist[a][b] = dist[b][a] = 1;
        }
        for (int c = 0; c < N; c++)
            for (int a = 0; a < N; a++)
                for (int b = 0; b < N; b++)
                    CheckMin(ref dist[a][b], dist[a][c] + dist[c][b]);

        long ans = 0;
        int[] cand = new int[N + 1];
        int V = 1;
        var dp = new int[1 << K + 1][];
        for (int r = 0; r < dp.Length; r++) dp[r] = new int[K + 1];

        for (int i = N - 1; i > 0 && V <= K; i--)
        {
            cand[V++] = i;
            for (int r = 0; r < dp.Length; r++)
                for (int c = 0; c < dp[r].Length; c++) dp[r][c] = K + 1;
            dp[1 << 0][0] = 0;
            for (int visited = 0; visited < (1 << V); visited++)
                for (int last = 0; last < V; last++)
                    if ((visited >> last & 1) == 1)
                        for (int next = 0; next < V; next++)
                            if ((visited >> next & 1) == 0)
                            {
                                int d = dist[cand[last]][cand[next]];
                                CheckMin(ref dp[visited | 1 << next][next], dp[visited][last] + d);
                            }
            int minUsed = K + 1;
            for (int last = 0; last < V; last++) CheckMin(ref minUsed, dp[(1 << V) - 1][last]);
            if (minUsed <= K) ans += (1L << i) - 1;
            else V--;
        }

        Console.WriteLine(ans);
    }

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


    Reader reader = new Reader(Console.In);
    static void Main() { new Program().Solve(); }
}

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 Reader(string file) { reader = new StreamReader(file); }
    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 Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public int[][] IntGrid(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public string[] StringArray(int N) { return Enu.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