結果
問題 | No.95 Alice and Graph |
ユーザー | eitaho |
提出日時 | 2015-03-15 17:04:27 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 3,629 ms / 5,000 ms |
コード長 | 3,674 bytes |
コンパイル時間 | 2,701 ms |
コンパイル使用メモリ | 118,512 KB |
実行使用メモリ | 35,276 KB |
最終ジャッジ日時 | 2024-11-14 13:32:22 |
合計ジャッジ時間 | 15,820 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,288 ms
35,276 KB |
testcase_01 | AC | 251 ms
33,232 KB |
testcase_02 | AC | 246 ms
31,064 KB |
testcase_03 | AC | 303 ms
33,108 KB |
testcase_04 | AC | 1,814 ms
35,024 KB |
testcase_05 | AC | 1,487 ms
32,980 KB |
testcase_06 | AC | 1,775 ms
35,276 KB |
testcase_07 | AC | 247 ms
35,032 KB |
testcase_08 | AC | 33 ms
25,420 KB |
testcase_09 | AC | 31 ms
25,300 KB |
testcase_10 | AC | 32 ms
25,264 KB |
testcase_11 | AC | 34 ms
27,184 KB |
testcase_12 | AC | 34 ms
27,212 KB |
testcase_13 | AC | 3,629 ms
31,184 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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++]; } }