結果

問題 No.95 Alice and Graph
ユーザー eitahoeitaho
提出日時 2015-03-15 16:52:30
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,791 bytes
コンパイル時間 3,397 ms
コンパイル使用メモリ 111,428 KB
実行使用メモリ 84,164 KB
最終ジャッジ日時 2023-09-11 08:22:23
合計ジャッジ時間 16,333 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 430 ms
28,912 KB
testcase_02 AC 422 ms
28,924 KB
testcase_03 AC 65 ms
21,852 KB
testcase_04 WA -
testcase_05 AC 1,744 ms
46,520 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 64 ms
21,772 KB
testcase_09 AC 64 ms
21,812 KB
testcase_10 AC 63 ms
23,812 KB
testcase_11 AC 66 ms
23,704 KB
testcase_12 AC 66 ms
21,844 KB
testcase_13 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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++)
                    dist[a][b] = Math.Min(dist[a][b], dist[a][c] + dist[c][b]);

        long ans = 0;
        int lastUsed = 0;
        var cand = new List<int>();
        cand.Add(0);
        for (int i = N - 1; i > 0 && lastUsed < K; i--)
        {
            cand.Add(i);
            int V = cand.Count;
            var dp = new int[1 << V, V];
            Fill(dp, 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 (last != next)
                            {
                                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; lastUsed = minUsed; }
            else cand.RemoveAt(V - 1);
        }

        Console.WriteLine(ans);
    }

    void CheckMin(ref int a, int 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;
    }



    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