結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2016-04-27 00:53:30 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,546 bytes |
| コンパイル時間 | 836 ms |
| コンパイル使用メモリ | 108,928 KB |
| 実行使用メモリ | 285,332 KB |
| 最終ジャッジ日時 | 2024-10-04 15:58:28 |
| 合計ジャッジ時間 | 8,054 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 3 TLE * 1 -- * 22 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
class Program
{
public void Proc()
{
Reader.IsDebug = false;
int[] inpt = Reader.GetInt();
int ekiCount = inpt[0];
int roadCount = inpt[1];
this.StartEki = inpt[2];
this.GoalEki = inpt[3];
Dictionary<int, Dictionary<int, long>> dic = new Dictionary<int,Dictionary<int, long>>();
for(int i=0; i<roadCount; i++) {
inpt = Reader.GetInt();
if(!dic.ContainsKey(inpt[0])) {
dic.Add(inpt[0], new Dictionary<int, long>());
}
dic[inpt[0]].Add(inpt[1], inpt[2]);
if(!dic.ContainsKey(inpt[1])) {
dic.Add(inpt[1], new Dictionary<int, long>());
}
dic[inpt[1]].Add(inpt[0], inpt[2]);
}
Queue<TaskItem> task = new Queue<TaskItem>();
TaskItem firstTask = new TaskItem(StartEki);
task.Enqueue(firstTask);
Keiro[] step = new Keiro[ekiCount + 1];
step[StartEki] = firstTask.Route;
while (task.Count > 0)
{
TaskItem item = task.Dequeue();
foreach (int key in dic[item.Pos].Keys)
{
Keiro kei = item.Route.Duplicate();
kei.Add(key);
kei.AddCost(dic[item.Pos][key]);
if(step[key] == null || step[key].Cost > kei.Cost) {
step[key] = kei;
TaskItem tsk = new TaskItem(key, kei);
task.Enqueue(tsk);
} else if(step[key].Cost == kei.Cost && kei.CompareTo(step[key]) < 0) {
step[key] = kei;
TaskItem tsk = new TaskItem(key, kei);
task.Enqueue(tsk);
}
}
}
Console.WriteLine(string.Join(" ", step[GoalEki]));
}
private int StartEki;
private int GoalEki;
public class TaskItem
{
public Keiro Route;
public int Pos;
public TaskItem(int pos) {
this.Route = new Keiro();
this.Route.Add(pos);
this.Pos = pos;
}
public TaskItem(int pos, Keiro kei) {
this.Route = kei;
this.Pos = pos;
}
}
public class Keiro : List<int>, IComparable<List<int>>
{
public long Cost {
get {
return costValue;
}
}
public void AddCost(long addCost) {
this.costValue+=addCost;
}
private long costValue = 0;
public int CompareTo(List<int> other)
{
for(int i=0; i<Math.Min(this.Count, other.Count); i++) {
if(this[i] < other[i]) {
return -1;
} else if(this[i] > other[i]) {
return 1;
}
}
if(this.Count < other.Count) {
return -1;
} else if(this.Count > other.Count) {
return 1;
}
return 0;
}
public Keiro Duplicate() {
Keiro newIns = new Keiro();
newIns.AddRange(this);
newIns.AddCost(this.costValue);
return newIns;
}
}
public class Reader
{
public static bool IsDebug = true;
private static String PlainInput = @"
";
private static System.IO.StringReader Sr = null;
public static string ReadLine()
{
if (IsDebug)
{
if (Sr == null)
{
Sr = new System.IO.StringReader(PlainInput.Trim());
}
return Sr.ReadLine();
}
else
{
return Console.ReadLine();
}
}
public static int[] GetInt(char delimiter = ' ', bool trim = false)
{
string inptStr = ReadLine();
if (trim)
{
inptStr = inptStr.Trim();
}
string[] inpt = inptStr.Split(delimiter);
int[] ret = new int[inpt.Length];
for (int i = 0; i < inpt.Length; i++)
{
ret[i] = int.Parse(inpt[i]);
}
return ret;
}
}
static void Main()
{
Program prg = new Program();
prg.Proc();
}
}
14番