結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 37zigen37zigen
提出日時 2016-05-01 01:00:49
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,400 bytes
コンパイル時間 2,467 ms
コンパイル使用メモリ 82,532 KB
実行使用メモリ 62,100 KB
最終ジャッジ日時 2024-04-15 08:13:52
合計ジャッジ時間 13,081 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 198 ms
55,748 KB
testcase_01 AC 193 ms
55,692 KB
testcase_02 AC 196 ms
55,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 254 ms
59,064 KB
testcase_28 WA -
testcase_29 AC 252 ms
57,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int s=sc.nextInt();
		int g=sc.nextInt();
		Dijkstra dj=new Dijkstra(n);
		for(int i=0;i<m;i++){
			int from=sc.nextInt();
			int to=sc.nextInt();
			int cost=sc.nextInt();
			dj.addEdge(from, to, cost);
		}
		int[] prv=dj.getShortestPath(s, g);
		String ans="";
		ans+=g;
		int f=g;
		while(true){
			int a=prv[f];
			f=a;
			if(a!=-1){
				ans=a+" "+ans;
				if(a==s)break;
			}
		}
		System.out.println(ans);
//		tr(prv);

	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
	class Dijkstra{
		int n;
		long[] d;
		int s;
		int[] prv;
		List<Edge>[]edges;
		PriorityQueue<Vertice> pq;
		final long INF=Long.MAX_VALUE/4;
		Dijkstra(int n){
			this.n=n;
			d=new long[n];
			prv=new int[n];
			Arrays.fill(prv, -1);
			@SuppressWarnings("unchecked")
			List<Edge>[]edges=new List[n];
			this.edges=edges;
			for(int i=0;i<n;i++){
				edges[i]=new ArrayList<Edge>();
			}
			s=-1;
		}
		int[] getShortestPath(int i,int j){
			if(i!=s){
				s=i;
				Arrays.fill(d, INF);
				d[i]=0;
				pq=new PriorityQueue<Vertice>();
				pq.add(new Vertice(i,d[i]));
				while(!pq.isEmpty()){
					Vertice u=pq.poll();
					if(d[u.me]<u.cost){
						continue;//skip old vertex
					}
					for(int ii=0;ii<edges[u.me].size();ii++){
						Edge e=edges[u.me].get(ii);
						if(d[e.from]!=INF&&e.cost+d[e.from]<d[e.to]){
							d[e.to]=e.cost+d[e.from];
							pq.add(new Vertice(e.to,d[e.to]));
							prv[e.to]=e.from;
						}
					}
				}
				s=i;
			}
			//			return d[j];
			return prv;
		}
		void addEdge(int from,int to,long cost){
			edges[from].add(new Edge(from,to,cost));
		}
	}
	class Edge{
		int from;
		int to;
		long cost;
		Edge(int i,int j,long cost){
			this.from=i;
			this.to=j;
			this.cost=cost;
		}
	}
	class Vertice implements Comparable<Vertice>{
		int me;
		long cost;
		Vertice(int me,long cost){
			this.me=me;
			this.cost=cost;
		}
		@Override
		public int compareTo(Vertice o){
			//			return Long.compare(this.cost, o.cost);
			return this.cost>o.cost?1:this.cost<o.cost?-1:this.me>o.me?1:this.me<o.me?-1:0;
		}
	}
}
0