結果
| 問題 |
No.340 雪の足跡
|
| コンテスト | |
| ユーザー |
eitaho
|
| 提出日時 | 2016-03-23 02:00:05 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,010 bytes |
| コンパイル時間 | 2,490 ms |
| コンパイル使用メモリ | 110,464 KB |
| 実行使用メモリ | 434,148 KB |
| 最終ジャッジ日時 | 2024-10-01 16:49:07 |
| 合計ジャッジ時間 | 10,271 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 11 TLE * 5 -- * 16 |
コンパイルメッセージ
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.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;
class Program
{
static readonly string Impossible = "Odekakedekinai..";
public void Solve()
{
int W = Reader.Int(), H = Reader.Int(), N = Reader.Int();
var table = Reader.IntTable(N * 2);
//var dij = new Dijkstra(H * W);
var G = new List<Edge>[H * W];
for (int i = 0; i < G.Length; i++)
G[i] = new List<Edge>();
for (int ti = 1; ti < N * 2; ti += 2)
{
var t = table[ti];
for (int i = 0; i + 1 < t.Length; i++)
{
int r = t[i] / W, c = t[i] % W;
int nr = t[i + 1] / W, nc = t[i + 1] % W;
int dr = Math.Sign(nr - r), dc = Math.Sign(nc - c);
for (; r != nr || c != nc; r += dr, c += dc)
{
G[r * W + c].Add(new Edge((r + dr) * W + (c + dc), 1));
G[(r + dr) * W + (c + dc)].Add(new Edge(r * W + c, 1));
}
}
}
int ans = ShortestPath(0, G).Last();
Console.WriteLine(ans == INF ? Impossible : ans + "");
}
static readonly int INF = (int)1e9;
static int[] ShortestPath(int start, List<Edge>[] G)
{
int N = G.Length;
bool[] inQue = new bool[N];
var que = new Queue<int>();
que.Enqueue(start);
var minCost = new int[N];
for (int i = 0; i < N; i++) minCost[i] = INF;
minCost[start] = 0;
while (que.Count > 0)
{
int v = que.Dequeue();
inQue[v] = false;
foreach (var e in G[v])
if (minCost[v] + e.cost < minCost[e.to])
{
minCost[e.to] = minCost[v] + e.cost;
if (!inQue[e.to])
{
inQue[e.to] = true;
que.Enqueue(e.to);
}
}
}
return minCost;
}
struct Edge
{
public int to;
public int cost;
public Edge(int to, int cost) { this.to = to; this.cost = cost; }
}
}
class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
private static TextReader reader = Console.In;
private static readonly char[] separator = { ' ' };
private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
private static string[] A = new string[0];
private static int i;
private static void Init() { A = new string[0]; }
public static void Set(TextReader r) { reader = r; Init(); }
public static void Set(string file) { reader = new StreamReader(file); Init(); }
public static bool HasNext() { return CheckNext(); }
public static string String() { return Next(); }
public static int Int() { return int.Parse(Next()); }
public static long Long() { return long.Parse(Next()); }
public static double Double() { return double.Parse(Next()); }
public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
public static string Line() { return reader.ReadLine().Trim(); }
private static string[] Split(string s) { return s.Split(separator, op); }
private static string Next() { CheckNext(); return A[i++]; }
private static bool CheckNext()
{
if (i < A.Length) return true;
string line = reader.ReadLine();
if (line == null) return false;
if (line == "") return CheckNext();
A = Split(line);
i = 0;
return true;
}
}
eitaho