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.this
Each input file contains one test case. For each case, the first line contains two positive integers $Nv (≤10^3) $and$ Ne (≤10^5)$, which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv.spa
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.code
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.blog
All the inputs in a line are separated by a space.three
For each of the K sequences, print in a line Yes
if it is a Dijkstra sequence, or No
if not.ci
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
Yes Yes Yes No
给定一个Nv个顶点,Ne条边的图,K个查询,每一次查询给定一个Nv个长度的顶点序列,判断该顶点序列是不是Dijkstra sequence,若是是输出Yes, 不然输出No。input
首先使用query数组存储每一次查询的顶点序列,那么query[0]
即为源点,使用常规的Dijkstra算法求解每个点到query[0]
的最短距离,并在每次在为选择的顶点集合中选择距离源点最短的距离的时候,将该距离添加进chosenDist
数组中,这样,chosenDist
数组就保存了每一次选择的最短距离,而后再遍历query数组,判断dis[query[j]]
和chosenDist[j]
是否所有相等,若是是输出Yes,不然输出No。这么作之因此可行,是由于Dijkstra算法不变的量就是第k次选择出来的最短距离是不变的,不管第k个顶点选择哪一个。string
#include<cstdio> #include<vector> #include<cstring> using namespace std; int Nv,Ne;// 顶点数和边数 int G[1005][1005]; int dis[1005];// 保存每个节点的最短距离 bool visited[1005]; vector<int> chosenDist;// 保存每一次选择的最短距离 void Dijkstra(int start){ fill(dis,dis+1005,0x3fffffff); dis[start] = 0; for(int i=0;i<Nv;++i){ int min_dis = 0x3fffff; int min_index = -1; for(int j=1;j<=Nv;++j){ if(!visited[j]&&min_dis>dis[j]){ min_dis = dis[j]; min_index = j; } } if(min_index==-1) return; visited[min_index] = true; // 将每次选择的最短距离添加到chosenDist中。 chosenDist.push_back(min_dis); for(int j=1;j<=Nv;++j){ if(!visited[j]&&G[min_index][j]!=0&&dis[min_index]+G[min_index][j]<dis[j]){ dis[j] = dis[min_index]+G[min_index][j]; } } } } int main(){ scanf("%d %d",&Nv,&Ne); for(int i=0;i<Ne;++i){ int a,b,d; scanf("%d %d %d",&a,&b,&d); G[a][b] = G[b][a] = d; } int K; scanf("%d",&K); for(int i=0;i<K;++i){ int query[Nv+1]; for(int j=0;j<Nv;++j){ scanf("%d",&query[j]); } chosenDist.clear(); memset(visited,0,sizeof(visited)); Dijkstra(query[0]); bool isTrue = true; for(int j=0;j<Nv;++j){ if(dis[query[j]]!=chosenDist[j]){ isTrue = false; break; } } if(isTrue){ printf("Yes\n"); }else{ printf("No\n"); } } return 0; }