結果

問題 No.17 2つの地点に泊まりたい
ユーザー holegumaholeguma
提出日時 2015-08-08 18:14:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 122 ms / 5,000 ms
コード長 2,360 bytes
コンパイル時間 2,724 ms
コンパイル使用メモリ 81,880 KB
実行使用メモリ 52,340 KB
最終ジャッジ日時 2024-04-23 02:06:37
合計ジャッジ時間 4,906 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
50,308 KB
testcase_01 AC 50 ms
50,556 KB
testcase_02 AC 50 ms
50,400 KB
testcase_03 AC 58 ms
50,652 KB
testcase_04 AC 61 ms
51,080 KB
testcase_05 AC 113 ms
52,300 KB
testcase_06 AC 97 ms
52,032 KB
testcase_07 AC 85 ms
51,560 KB
testcase_08 AC 115 ms
52,092 KB
testcase_09 AC 122 ms
52,240 KB
testcase_10 AC 87 ms
51,828 KB
testcase_11 AC 103 ms
52,340 KB
testcase_12 AC 51 ms
50,672 KB
testcase_13 AC 50 ms
50,096 KB
testcase_14 AC 51 ms
50,568 KB
testcase_15 AC 51 ms
50,536 KB
testcase_16 AC 49 ms
50,216 KB
testcase_17 AC 51 ms
50,292 KB
testcase_18 AC 55 ms
50,572 KB
testcase_19 AC 53 ms
50,464 KB
testcase_20 AC 51 ms
50,512 KB
testcase_21 AC 50 ms
50,312 KB
testcase_22 AC 55 ms
50,632 KB
testcase_23 AC 94 ms
51,704 KB
testcase_24 AC 86 ms
51,996 KB
testcase_25 AC 51 ms
50,140 KB
testcase_26 AC 111 ms
52,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Main{

static final int INF=Integer.MAX_VALUE/2;
static final PrintWriter out=new PrintWriter(System.out);

static class Node{
int d;
int v;
Node(int d,int v){
this.d=d; this.v=v;
}
static final Comparator<Node> DISTANCE_ORDER=new DistanceOrderComparator();
public static class DistanceOrderComparator implements Comparator<Node>{
public int compare(Node n1,Node n2){
return (n1.d>n2.d)?1:(n1.d<n2.d)?-1:0;
}
}
}

static class Edge{
int to;
int cost;
Edge(int to,int cost){
this.to=to; this.cost=cost;
}
}

static class MyArrayList extends ArrayList<Edge>{}

public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line="";
while((line=br.readLine())!=null&&!line.isEmpty()){
int n=Integer.parseInt(line);
MyArrayList[] array=new MyArrayList[n];
int[] s=new int[n];
int[][] d=new int[n][n];
for(int i=0;i<n;i++) s[i]=Integer.parseInt(br.readLine());
int m=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++) array[i]=new MyArrayList();
for(int i=0;i<m;i++){
line=br.readLine();
StringTokenizer st=new StringTokenizer(line);
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
int c=Integer.parseInt(st.nextToken());
array[a].add(new Edge(b,c));
array[b].add(new Edge(a,c));
}
for(int i=0;i<n;i++) dijkstra(i,d,array);
long ans=Long.MAX_VALUE;
for(int i=1;i<n-1;i++){
if(d[0][i]==INF) continue;
for(int j=1;j<n-1;j++){
if(i==j||d[i][j]==INF||d[j][n-1]==INF) continue;
ans=Math.min(ans,d[0][i]+s[i]+d[i][j]+s[j]+d[j][n-1]);
}
}
out.println(ans);
out.flush();
}
}
private static void dijkstra(int s,int[][] d,MyArrayList[] array){
PriorityQueue<Node> pque=new PriorityQueue<Node>(1,Node.DISTANCE_ORDER);
Arrays.fill(d[s],INF);
d[s][s]=0;
pque.offer(new Node(0,s));
while(!pque.isEmpty()){
Node n=pque.poll();
int v=n.v;
ArrayList<Edge> list=array[v];
if(d[s][v]<n.d||list.size()==0) continue;
for(int i=0;i<list.size();i++){
Edge e=list.get(i);
int to=e.to;
int dv=d[s][v];
int cost=e.cost;
if(d[s][to]>dv+cost){
d[s][to]=dv+cost;
pque.offer(new Node(d[s][to],to));
}
}
}
}
}
0