結果

問題 No.614 壊れたキャンパス
ユーザー ikdikd
提出日時 2017-12-14 16:56:29
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 991 ms / 2,000 ms
コード長 2,229 bytes
コンパイル時間 1,076 ms
コンパイル使用メモリ 119,920 KB
実行使用メモリ 56,820 KB
最終ジャッジ日時 2023-09-03 17:35:44
合計ジャッジ時間 11,910 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 885 ms
53,924 KB
testcase_09 AC 890 ms
54,908 KB
testcase_10 AC 739 ms
56,820 KB
testcase_11 AC 958 ms
53,140 KB
testcase_12 AC 991 ms
53,488 KB
testcase_13 AC 973 ms
55,208 KB
testcase_14 AC 912 ms
56,172 KB
testcase_15 AC 680 ms
52,924 KB
testcase_16 AC 845 ms
53,212 KB
testcase_17 AC 569 ms
52,644 KB
testcase_18 AC 635 ms
48,896 KB
testcase_19 AC 544 ms
49,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, m; long k; int s, t;
  rd(n, m, k, s, t);
  s--; t--;
  auto as=new int[](m), from=new int[](m), to=new int[](m);
  foreach(i; 0..m)
    rd(as[i], from[i], to[i]), as[i]--, from[i]--, to[i]--;

  int[long] map;
  map[s]=0; map[(n-1)*k+t]=1;
  foreach(i; 0..m){
    if((as[i]*k+from[i] in map)==null) map[as[i]*k+from[i]]=map.length.to!int; 
    if(((as[i]+1)*k+to[i] in map)==null) map[(as[i]+1)*k+to[i]]=map.length.to!int; 
  }
  import std.typecons;
  alias Edge=Tuple!(int, "to", int, "cost");
  auto vmax=map.length;
  auto eds=new Edge[][](vmax, 0);
  foreach(i; 0..m){
    auto u=map[as[i]*k+from[i]],
         v=map[(as[i]+1)*k+to[i]];
    eds[u]~=Edge(v, 0);
  }
  auto blds=new int[][](n, 0);
  blds[0]~=s; blds[n-1]~=t;
  foreach(i; 0..m){
    blds[as[i]]~=from[i];
    blds[as[i]+1]~=to[i];
  }
  foreach(i; 0..n){
    auto b=blds[i];
    sort(b);
    for(auto j=0, pre=-1; j<b.length; j++){
      while(j+1<b.length && b[j]==b[j+1]) j++;
      if(pre>=0){
        auto u=map[i*k+b[pre]], v=map[i*k+b[j]];
        auto d=b[j]-b[pre];
        eds[u]~=Edge(v, d);
        eds[v]~=Edge(u, d);
      }
      pre=j;
    }
  }

  auto dist=new long[](vmax);
  const inf=1_000_000_000_000_000_000;
  fill(dist, inf);
  alias Node=Tuple!(int, "v", long, "d");
  import std.container;
  auto Q=new BinaryHeap!(Array!Node, "a.d>b.d");
  Q.insert(Node(map[s], 0)); dist[map[s]]=0;
  while(Q.length>0){
    auto cur=Q.front; Q.removeFront;
    foreach(e; eds[cur.v]){
      if(dist[e.to]<=dist[cur.v]+e.cost) continue;
      dist[e.to]=dist[cur.v]+e.cost;
      Q.insert(Node(e.to, dist[e.to]));
    }
  }

  auto d=dist[map[(n-1)*k+t]];
  if(d<inf) writeln(d);
  else writeln(-1);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
void wr(T...)(T x){
  import std.stdio;
  foreach(e; x) stderr.write(e, " ");
  stderr.writeln();
}
void prtup(T)(T tup){
  import std.stdio;
  foreach(i, e; tup){
    stderr.writeln(i);
    foreach(ee; e){
      foreach(eee; ee) stderr.write(eee, " ");
      stderr.writeln();
    }
  }
}
0