結果

問題 No.1 道のショートカット
ユーザー iicafiaxusiicafiaxus
提出日時 2016-09-02 00:23:11
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,589 bytes
コンパイル時間 1,080 ms
コンパイル使用メモリ 132,732 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 04:07:35
合計ジャッジ時間 2,357 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

class Edge{
	Node from;
	Node to;
	int cost;
	int time;
	this(Node from, Node to, int cost, int time){
		this.from = from;
		this.to = to;
		this.cost = cost;
		this.time = time;
		to.inedges ~= this;
		}
	}

class Node{
	Edge[] inedges;
	int[int] xs; // cost -> time
	this(){
		}
	}	

void main(){
	int n = readln.chomp.to!int;
	int c = readln.chomp.to!int;
	int v = readln.chomp.to!int;
	int[] ss = readln.chomp.split.map!(to!int).map!(x => x - 1).array;
	int[] ts = readln.chomp.split.map!(to!int).map!(x => x - 1).array;
	int[] ys = readln.chomp.split.map!(to!int).array;
	int[] ms = readln.chomp.split.map!(to!int).array;
	
	Node[] nodes;
	nodes ~= new Node;
	nodes[0].xs[0] = 0;
	foreach(i; 0 .. n) nodes ~= new Node;
	
	Edge[] edges;
	for(int i = 0; i < v; i ++){
		edges ~= new Edge(nodes[ss[i]], nodes[ts[i]], ys[i], ms[i]);
		}
	
	foreach(no; nodes){
		foreach(ed; no.inedges){
			foreach(cq; ed.from.xs.keys){
				if(cq + ed.cost <= c){
					if(!(cq + ed.cost in no.xs) || no.xs[cq + ed.cost] > ed.from.xs[cq] + ed.time){
						no.xs[cq + ed.cost] = ed.from.xs[cq] + ed.time;
						}
					}
				}
			}
		int thr = int.max;
		int[] ks = no.xs.keys;
		ks.sort!("a < b")();
		foreach(k; ks){
			if(no.xs[k] >= thr) no.xs.remove(k);
			else thr = no.xs[k];
			}
		}
	
	int ans = -1;
	if(nodes[n - 1].xs.keys.length){
		int min = int.max;
		foreach(t; nodes[n - 1].xs){
			if(t < min) min = t;
			}
		ans = min;
		}
	
	ans.writeln;
	
	}
0