-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1163_Dijkstra Sequence (30).cpp
52 lines (43 loc) · 1.37 KB
/
1163_Dijkstra Sequence (30).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<pair<int, int>> G[1005]; // adjacent list(133ms) is better than matrix(246ms) here.
bool check(const vector<int> &path) {
int vcnt = path.size();
vector<int> dis(vcnt+1, 0x3fffffff);
unordered_map<int, int> min_set;
dis[path[0]] = 0;
for (int i = 0; i < vcnt-1; i++) {
int from = path[i];
min_set.erase(from); // remove the current seed node
for (const auto &to : G[from]) {
if (dis[from] + to.second < dis[to.first]) {
dis[to.first] = dis[from] + to.second;
min_set[to.first] = dis[to.first];
}
}
int min_dis = 0x3fffffff;
for (auto &s : min_set) min_dis = min(min_dis, s.second);
// if next candidate node not match the minimum distance condition
if (dis[path[i+1]] != min_dis) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
int v, e, a, b, d, k;
cin >> v >> e;
for (int i = 0; i < e; i++) {
cin >> a >> b >> d;
G[a].push_back(make_pair(b, d));
G[b].push_back(make_pair(a, d));
}
vector<int> path(v);
cin >> k;
for (int i = 0; i < k; i++) {
for (int j = 0; j < v; j++) cin >> path[j];
cout << (check(path) ? "Yes" : "No") << endl;
}
return 0;
}