結果

問題 No.95 Alice and Graph
ユーザー nanophoto12nanophoto12
提出日時 2014-12-22 01:46:56
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,616 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 103,840 KB
実行使用メモリ 29,412 KB
最終ジャッジ日時 2023-09-02 21:25:35
合計ジャッジ時間 21,673 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,793 ms
27,476 KB
testcase_01 AC 356 ms
27,452 KB
testcase_02 AC 350 ms
27,472 KB
testcase_03 AC 344 ms
23,372 KB
testcase_04 AC 2,756 ms
27,448 KB
testcase_05 AC 2,109 ms
25,456 KB
testcase_06 AC 2,702 ms
25,364 KB
testcase_07 AC 357 ms
27,460 KB
testcase_08 AC 62 ms
19,752 KB
testcase_09 AC 62 ms
23,928 KB
testcase_10 AC 60 ms
19,832 KB
testcase_11 AC 64 ms
21,732 KB
testcase_12 AC 65 ms
23,324 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.Collections.Generic;
using System.Linq;

namespace Yukicoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var firstLine = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var n = firstLine[0];
            var m = firstLine[1];
            var k = firstLine[2];

            int[,] graph = new int[n, n];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    graph[i, j] = 1000;
                }                
            }
            for (int i = 0; i < n; i++)
            {
                graph[i, i] = 0;                
            }

            for (int i = 0; i < m; i++)
            {
                var line = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                graph[line[0]-1, line[1]-1] = 1;
                graph[line[1]-1, line[0]-1] = 1;
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    for (int l = 0; l < n; l++)
                    {
                        graph[l, j] = Math.Min(graph[l, j], graph[l, i] + graph[i, j]);
                    }
                }
            }

            List<int> vertexs = new List<int>();
            vertexs.Add(0);
            long score = 0L;
            int[,] dp = new int[1 << 16, 16];
            for (int i = n - 1; i > 0; i--)
            {
                if (vertexs.Count >= k + 1)
                {
                    break;
                }
                vertexs.Add(i);
                for (int j = 0; j < 1 << 16; j++)
                {
                    for (int l = 0; l < 16; l++)
                    {
                        dp[j, l] = 0x3f;
                    }
                }
                dp[1, 0] = 0;
                int count = vertexs.Count;
                for (int mask = 1; mask < 1 << count; mask++)
                {
                    for (int x = 0; x < count; x++)
                    {
                        if ((mask & (1 << x)) == (1<<x))
                        {
                            for (int y = 0; y < count; y++)
                            {
                                if (x == y)
                                {
                                    continue;
                                }
                                if ((mask & (1 << y)) == (1 << y))
                                {
                                    //移動回数をメモする
                                    var element = dp[mask ^ (1 << x), y] + graph[vertexs[x], vertexs[y]];
                                    if (element < dp[mask, x])
                                    {
                                        dp[mask, x] = element;
                                    }
                                }
                            }
                        }
                    }
                }
                bool canMove = false;
                int count1 = vertexs.Count;
                for (int j = 0; j < count1; j++)
                {
                    if (dp[(1 << count1) - 1, j] <= k)
                    {
                        canMove = true;
                    }
                }
                if (!canMove)
                {
                    vertexs.Remove(i);
                }
                else
                {
                    score += (1L << i) - 1L;
                }
            }
            Console.WriteLine(score);
        }
    }
}
0