Dijkstra's algorithm is one of the very famous greedy algorithms.
It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.
In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let's call it Dijkstra sequence, is generated by Dijkstra's algorithm.
On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers Nv (≤103) and Ne (≤105), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv.
Then Ne lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the Nv vertices. It is assumed that the first vertex is the source for each sequence.
All the inputs in a line are separated by a space.
Output Specification:
For each of the K sequences, print in a line Yes
if it is a Dijkstra sequence, or No
if not.
Sample Input:
5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4
Sample Output:
Yes
Yes
Yes
No
除了常规的迪杰斯特拉算法求最短路径之外,要注意本题在当 存在dis[]不唯一时,要考虑所有情况,也就是说最短路径长度相同,但路径会有所不同,Dijkstra sequence的序列可能会与常规的最短路径遍历的序列不同,但可能是正确的。
所以要进行如下处理
if(u==-1){
continue;
}else{
if(dis[sequence[l]]==min)//将给的dijstra序列当前的值的dis值yeshi与
u=sequence[l]; //当前得到的最小路径长度相 比,如果相等,
//就说明给出的结点是也是正确的,将u替换为该序列中的结点值
}
完整代码如下
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int Vnum=sc.nextInt(),Enum=sc.nextInt();
boolean[] vis=new boolean[Vnum+1];
int[] dis=new int[Vnum+1];
int[][] edge=new int[Vnum+1][Vnum+1];
final int inf=Integer.MAX_VALUE;
for(int i=1;i<=Vnum;i++){
Arrays.fill(edge[i],inf);
}
for(int i=0;i<Enum;i++){
int from,to,w;
from=sc.nextInt();
to=sc.nextInt();
w=sc.nextInt();
edge[from][to]=edge[to][from]=w;
}
int k=sc.nextInt();
while((k--)!=0){
Arrays.fill(vis,false);
Arrays.fill(dis,inf);
int[] sequence=new int[Vnum];
int[] getSeq=new int[Vnum];
int count=0;
for(int i=0;i<Vnum;i++){
sequence[i]=sc.nextInt();
}
int current=sequence[0];
dis[current]=0;
for(int l=0;l<Vnum;l++){
int u=-1,min=inf;
for(int i=1;i<=Vnum;i++) {
if (vis[i] == false && dis[i] < min) {
min = dis[i];
u = i;
}
}
if(u==-1){
continue;
}else{
if(dis[sequence[l]]==min)
u=sequence[l];
}
vis[u]=true;
getSeq[count++]=u;
for(int v=1;v<=Vnum;v++){
if(vis[v]==false&&edge[u][v]!=inf){
if(dis[u]+edge[u][v]<dis[v]){
dis[v]=dis[u]+edge[u][v];
}
}
}
}
int j;
for(j=0;j<Vnum;j++){
if(getSeq[j]!=sequence[j]){
System.out.println("No");
break;
}
}
if(j==Vnum)
System.out.println("Yes");
}
}
}