(PAT)1111. Online Map(迪傑斯特拉演算法) JAVA版
1111. Online Map (30) JAVA實現
看最近好多小夥伴對這個演算法非常苦惱又不知道該怎麼辦
所以抱著試試的心態修改了一份 閹割版 (強行搬運) 勉強能用
用例測試不要複製貼上 僅限手打。
PS: 用JAVA寫演算法是真的難受。 甘!QAQ
話不多說下面看題
時間限制
300 ms
記憶體限制
65536 kB
程式碼長度限制
16000 B
判題程式
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
Distance = D: source -> v1 -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u1 -> ... -> destination
import java.util.Scanner; import java.util.Stack; public class Main { private int [][] a=new int[510][510]; //求最短路徑是選擇 引數 a b private int [][] b=new int[510][510]; //最短時間 引數 b a private int [] sp=new int [510]; //記錄當前最短 private int [] st=new int [510]; //記錄當前最短 //前結點儲存在 a[i][i]中 private Integer inf=Integer.MAX_VALUE; //設定最大防止溢位 public static void main(String[] args) { Main a=new Main(); a.handlee(); } void dijkstra(int a[][],int b[][],int start,int end,int n) { int begin; boolean visit[]=new boolean[510]; //初始化,前序 for(int i=0;i<n;i++) { visit[i]=false; sp[i]=inf; st[i]=inf; } a[start][start]=start; sp[start]=0; st[start]=0; visit[start]=true; while(true) { //更新 for(int i=0;i<n;i++) { if(a[start][i]==-1) continue; if(sp[i]>a[start][i]+sp[start]) { sp[i]=a[start][i]+sp[start]; st[i]=b[start][i]+st[start]; //start=i;//這樣不全 a[i][i]=start; }else if(sp[i]==a[start][i]+sp[start]) { if(st[i]>b[start][i]+st[start]) { st[i]=b[start][i]+st[start]; a[i][i]=start; } } } int min=inf; begin=-1; //選擇起始點,必須從全域性找,這樣的貪心才是最 for(int i=0;i<n;i++) { if(!visit[i]&&sp[i]<min) { min=sp[i]; begin=i; } } if(begin==-1) break; start=begin; visit[start]=true; if(start==end) break; } } private void handlee(){ Scanner s1=new Scanner(System.in); for(int i=0;i<510;i++) { for(int j=0;j<510;j++) { a[i][j]=-1; b[i][j]=-1; } } int N,M; N=s1.nextInt(); M=s1.nextInt(); int v1,v2,one_way; for(int i=0;i<M;i++) { Scanner s2=new Scanner(System.in); v1 = s2.nextInt(); v2 = s2.nextInt(); one_way =s2.nextInt(); a[v1][v2] = s2.nextInt(); b[v1][v2] = s2.nextInt(); if(one_way==0) { a[v2][v1]=a[v1][v2]; b[v2][v1]=b[v1][v2]; } } int s,d; Scanner s3=new Scanner(System.in); s=s3.nextInt(); d=s3.nextInt(); boolean isSame=true; Stack st1= new Stack(); Stack st2= new Stack(); int Dis,Time; dijkstra(a,b,s,d,N); Dis=sp[d]; int t=d; st1.push(d); while(true) { if(a[t][t]==t) break; st1.push(a[t][t]); t=a[t][t]; } dijkstra(b,a,s,d,N); Time=sp[d]; t=d; st2.push(d); while(true) { if(b[t][t]==t) break; if(b[t][t]!=a[t][t]) isSame=false; st2.push(b[t][t]); t=b[t][t]; } if(isSame) { System.out.print("Distance = "+Dis+"; Time = "+Time+": "); while(st1.empty()==false) { if((int) st1.peek()==d) System.out.print(st1.peek()); else System.out.print(st1.peek()+" -> "); st1.pop(); } }else { System.out.print("Distance = "+Dis +": "); while(st1.empty()==false) { if((int)st1.peek()==d) System.out.println(st1.peek()); else System.out.print(st1.peek()+" -> "); st1.pop(); } System.out.print("Time = "+Time+" : "); while(st2.empty()==false) { if((int)st2.peek()==d) System.out.println(st2.peek()); else System.out.print(st2.peek()+" -> "); st2.pop(); } } /// By Mr.L } // 還有問題的同學請不要XX我 自己想 }