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
分析:对每一个序列进行Dijkstra,判断过程依次判断该最短节点距离是否与输入序列对应节点相同,若相同则继续,否则false;
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<unordered_map>
//1009;
using namespace std;
typedef long long ll;
const int inf = 99999999;
int v, e;
int k;
int vt[1010], g[1010][1010], dis[1010];
bool dijkstra(int x) {
fill(dis, dis + 1010, inf);
bool visit[1010] = { false };
dis[x] = 0;
for (int i = 1; i <= v; i++) {
int minn = inf, index = -1;
for (int j = 1; j <= v; j++) {
if (!visit[j] && dis[j] < minn) {
minn = dis[j];
index = j;
}
}
if(dis[vt[i]]==minn) index = vt[i]; //按照vt数组顺序得到对应数字
else return false;
visit[index] = 1;
for (int j = 1; j <= v; j++) {
if (!visit[j] && g[index][j]>0) {
if (dis[j] > dis[index] + g[index][j]) {
dis[j] = dis[index] + g[index][j];
}
}
}
}
return true;
}
int main() {
cin >> v >> e;
int a, b, c;
for (int i = 0; i < e; i++) {
cin >> a >> b >> c;
g[a][b] = c;
g[b][a] = c;
}
cin >> k;
for (int i = 0; i < k; i++) {
fill(vt, vt + 1010, 0);
for (int j = 1; j <= v; j++) cin >> vt[j];
if (dijkstra(vt[1])) cout << "Yes" << endl;
else cout << "No" << endl;
}
}