7-4 Dijkstra Sequence (30分)
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 (≤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.
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
题目限制:
题目大意:
给定一个Nv个顶点,Ne条边的图,K个查询,每一次查询给定一个Nv个长度的顶点序列,判断该顶点序列是否是Dijkstra sequence,如果是输出Yes, 否则输出No。
算法思路:
首先使用query数组存储每一次查询的顶点序列,那么query[0]
即为源点,使用常规的Dijkstra算法求解每一个点到query[0]
的最短距离,并在每次在未选择的顶点集合中选择距离源点最短的距离的时候,将该距离添加进chosenDist
数组中,这样,chosenDist
数组就保存了每一次选择的最短距离,然后再遍历query数组,判断dis[query[j]]
和chosenDist[j]
是否全部相等,如果是输出Yes,否则输出No。这么做之所以可行,是因为Dijkstra算法不变的量就是第k次选择出来的最短距离是不变的,无论第k个顶点选择哪个。
提交结果:
AC代码:
#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;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。