結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 37zigen37zigen
提出日時 2016-05-01 12:26:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 613 ms / 5,000 ms
コード長 2,520 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 82,212 KB
実行使用メモリ 60,084 KB
最終ジャッジ日時 2024-04-15 08:18:52
合計ジャッジ時間 12,095 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 169 ms
55,160 KB
testcase_01 AC 168 ms
55,056 KB
testcase_02 AC 170 ms
55,132 KB
testcase_03 AC 176 ms
55,024 KB
testcase_04 AC 320 ms
59,348 KB
testcase_05 AC 387 ms
60,084 KB
testcase_06 AC 468 ms
59,608 KB
testcase_07 AC 276 ms
59,304 KB
testcase_08 AC 286 ms
59,948 KB
testcase_09 AC 281 ms
59,240 KB
testcase_10 AC 288 ms
59,824 KB
testcase_11 AC 297 ms
59,960 KB
testcase_12 AC 284 ms
59,832 KB
testcase_13 AC 275 ms
59,536 KB
testcase_14 AC 280 ms
59,832 KB
testcase_15 AC 274 ms
59,336 KB
testcase_16 AC 281 ms
59,320 KB
testcase_17 AC 285 ms
59,612 KB
testcase_18 AC 287 ms
59,280 KB
testcase_19 AC 283 ms
59,596 KB
testcase_20 AC 284 ms
59,920 KB
testcase_21 AC 272 ms
59,408 KB
testcase_22 AC 278 ms
59,508 KB
testcase_23 AC 294 ms
59,828 KB
testcase_24 AC 290 ms
59,796 KB
testcase_25 AC 280 ms
59,784 KB
testcase_26 AC 276 ms
59,204 KB
testcase_27 AC 241 ms
58,964 KB
testcase_28 AC 613 ms
59,876 KB
testcase_29 AC 224 ms
56,664 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);
			dj.addEdge(to, from, cost);
		}
		int[] prv=dj.getShortestPath(g, s);
		
		String ans="";
		ans+=s;
		int f=s;
		while(true){
			int a=prv[f];
			f=a;
			if(f!=-1){
				ans=ans+" "+a;
				if(a==g)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;
						}else if(d[e.to]==e.cost+d[e.from]&&prv[e.to]>e.from){
							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