1111. Online Map (30) JAVA实现
看最近好多小伙伴对这个算法非常苦恼又不知道该怎么办
所以抱着试试的心态修改了一份 阉割版 (强行搬运) 勉强能用
用例测试不要复制粘贴 仅限手打。
时间限制
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 {
int [][] a=new int[510][510]; //求最短路径是选择 参数 a b
int [][] b=new int[510][510]; //最短时间 参数 b a
int [] sp=new int [510]; //记录当前最短
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;
}
}
public 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();
}
}
}
}