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
AC代码:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long //可能会超时
#define PII pair<int,int>
const int INF = 0x3f3f3f3f, mod = 998244353;
const int N = 1005;
int n, m, g[N][N], mincost[N], vis[N];
int k, a[N];
bool dijk() {
mincost[a[0]] = 0;
for (int i = 0;i < n;i++) {
int t = a[i];
for (int j = 1;j <= n;j++) {
if (!vis[j] && mincost[j] < mincost[t]) return 0;
}
vis[t] = 1;
for (int j = 1;j <= n;j++)
mincost[j] = min(mincost[j], mincost[t] + g[t][j]);
}
return 1;
}
signed main()
{
ios::sync_with_stdio, cin.tie(0), cout.tie(0);
memset(g, 0x3f, sizeof g);
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = c;
}
cin >> k;
while (k--) {
memset(mincost, 0x3f, sizeof mincost);
memset(vis, 0, sizeof vis);
for (int i = 0;i < n;i++) cin >> a[i];
puts(dijk() ? "Yes" : "No");
}
return 0;
}
此题题意是给出无向图和k个序列,判断给定序列是否为Dijkstra序列,我们只需要在Dijkstra遍历序列每个点时判断是否有可以更新的点(距离更小的点)即可,有则说明不是Dijkstra序列,反之则是。注意到点数上限为1000,在朴素版n方加m和堆优化版mlogm的时间复杂度的情况下类似,选择了朴素版Dijkstra。
照顾英语不好的同学(bushi):
vertex n.顶点
greedy adj.贪婪的
hence adv.因此;之后
respectively adv.分别地,依次地
vertice n.顶点