結果

問題 No.95 Alice and Graph
ユーザー eitahoeitaho
提出日時 2015-03-15 17:00:43
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,660 bytes
コンパイル時間 2,951 ms
コンパイル使用メモリ 109,336 KB
実行使用メモリ 77,820 KB
最終ジャッジ日時 2023-09-11 08:24:33
合計ジャッジ時間 14,322 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,404 ms
77,820 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

        for (int i = N - 1; i > 0; i--)
        {
            cand[V++] = i;
            var dp = new int[1 << V][];
            for (int r = 0; r < dp.Length; r++)
            {
                dp[r] = new int[V];
                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