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
N
v
(
≤
1
0
3
)
N_v(≤10^3)
Nv(≤103) and
N
e
(
≤
1
0
5
)
N_e(≤10^5)
Ne(≤105), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to
N
v
N_v
Nv.
Then
N
e
N_e
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
≤100
≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K K K, is given as a positive integer no larger than 100, followed by K K K lines of sequences, each contains a permutationof the N v N_v 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
Caution:
最初一个一个判断新加入的点是不是距离最短的,但是超时了(但是结束后我查到的答案大部分都是这样做的,竟然也AC了),所以就优化了一下,先输入所有的path,然后按照 path 的第一个数字进行排序,这样就会使得以同一个点为起点的路径会放在一起,然后对这个点做一次 Dijkstra,判断后加入的点距起点的最短距离 dis 不会小于前面加入的点。
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-09-09 14:24:09
// All rights reserved.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
int edges[1010][1010];
vector<vector<int>> edges2;
int dis[1010];
int nv, ne;
struct Query{
vector<int> path;
int pos;
Query(){
path.resize(nv);
pos = -1;
}
~Query(){}
};
bool cmp(Query &a, Query &b){
return a.path[0] < b.path[0];
}
int main(){
scanf("%d %d", &nv, &ne);
edges2.resize(nv + 1);
for(int i = 0; i < ne; ++i){
int v1, v2;
scanf("%d %d", &v1, &v2);
scanf("%d", &edges[v1][v2]);
edges[v2][v1] = edges[v1][v2];
edges2[v1].push_back(v2);
edges2[v2].push_back(v1);
}
int k;
scanf("%d", &k);
vector<Query> queries(k);
for(int i = 0; i < k; ++i){
for(int j = 0; j < nv; ++j) scanf("%d", &queries[i].path[j]);
queries[i].pos = i;
}
sort(queries.begin(), queries.end(), cmp);
unordered_map<int, bool> ans;
bool vis[1010];
for(int i = 0; i < k; ++i){
ans[queries[i].pos] = true;
if(i == 0 || queries[i].path[0] != queries[i - 1].path[0]){
fill(dis, dis + 1010, 2147483647);
fill(vis, vis + 1010, false);
dis[queries[i].path[0]] = 0;
for(int j = 0; j < nv; ++j){
int u = -1, minDis = 2147483647;
for(int k = 0; k < nv; ++k){
if(vis[queries[i].path[k]]) continue;
if(dis[queries[i].path[k]] < minDis){
minDis = dis[queries[i].path[k]];
u = queries[i].path[k];
}
}
if(u == -1) break;
for(int x = 0; x < edges2[u].size(); ++x){
int v = edges2[u][x];
if(!vis[v] && dis[v] > dis[u] + edges[u][v]) dis[v] = dis[u] + edges[u][v];
}
vis[u] = true;
}
}
for(int j = 1; j < nv; ++j){
if(dis[queries[i].path[j]] < dis[queries[i].path[j - 1]]){
ans[queries[i].pos] = false;
break;
}
}
}
for(int i = 0; i < k; ++i){
if(ans[i]) printf("Yes\n");
else printf("No\n");
}
return 0;
}