原题链接:PAT 1163 Dijkstra Sequence(30)
关键词:Dijkstra
整理时间:2020.7.19
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
题目大意:
给出一个无向图和可能的序列,让你
判断
这个序列是否满足dijkstra序列(即通过dijkstra算法得到的)。
分析:
给出的序列是Dijkstra序列的关键在于,序列的第i个数x的dist[x]是等于当时求到的最短路径。
代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1010, INF = 0x3f3f3f;
int G[maxn][maxn], d[maxn], nv, ne, k; //G邻接矩阵存储图 d保存源点到各个顶点的当前最短路径长度
bool vis[maxn];
vector<int> path;
bool dijkstra(int s){
fill(d, d + maxn, INF);
fill(vis, vis + maxn, 0);
d[s] = 0;
for(int i = 0; i < nv; i++){
int MIN = INF, u = -1;
for(int j = 1; j <= nv; j++){
if(vis[j] == false && d[j] < MIN){ //j没访问过 且源点到j有路径
u = j;
MIN = d[j];
}
}
if(u == -1 || d[u] != d[path[i]]) return false;
vis[u] = true;
for(int v = 1; v <= nv; v++){
if(vis[v] == false && G[u][v] != INF){
if(d[u] + G[u][v] < d[v]){
d[v] = d[u] + G[u][v];
}
}
}
}
return true;
}
int main(){
cin >> nv >> ne;
fill(G[0], G[0] + maxn*maxn, INF); //注意,[0]一定要有
for(int i = 1; i <= ne; i++){
int a, b, w;
cin >> a >> b >> w;
G[a][b] = G[b][a] = w;
}
cin >> k;
for(int i = 0; i < k; i++){
path.clear();
for(int j = 0; j < nv; j++){
int a;
cin >> a;
path.push_back(a);
}
int st = path[0];
if(dijkstra(st)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
遇到的库函数:
- fill()对一个容器的值进行填充:
//填充一维数组
int array[8]; // 0 0 0 0 0 0 0 0
fill (array,array+4,5); //5 5 5 5 0 0 0 0
fill (array+3,array+6,8); //5 5 5 8 8 8 0 0
//填充vector
vector<int> v(8);
fill(v.begin(), v.begin()+4, 5); //5 5 5 5 0 0 0 0
fill(v.begin()+3, v.end()-2, 8); //5 5 5 8 8 8 0 0
//填充二维数组
int G[6][4];
fill(G[0], G[0] + 6*4, 520);