PAT 2019秋季 7-4 Dijkstra Sequence (30 分)
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算法计算从源点到达每个结点的距离d数组,判断当前所给序列后面的结点到源点的距离不小于之前一个结点到达源点距离,也就是d[i]<=d[i+1]才合法,所以只需要用Dijkstra算法计算出距离d数组,然后用循环判断即可。
AC代码
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int INF=(1<<30)-1;
const int N=1010;
int G[N][N],d[N],n,m,vist[N];
void Dijkstra(int x) {
fill(d,d+N,INF);
memset(vist,false,sizeof(vist));
d[x]=0;
for(int i=1; i<=n; i++) {
int MIN=INF,u=-1;
for(int j=1; j<=n; j++) {
if(!vist[j]&&d[j]<MIN) {
MIN=d[j];
u=j;
}
}
if(u==-1) return;
vist[u]=true;
for(int v=1; v<=n; v++) {
if(!vist[v]&&G[u][v]!=INF) {
if(d[u]+G[u][v]<d[v]) {
d[v]=G[u][v]+d[u];
}
}
}
}
}
int main() {
fill(G[0],G[0]+N*N,INF);
cin>>n>>m;
int a,b,weigh,k;
while(m--) {
scanf("%d%d%d",&a,&b,&weigh);
G[a][b]=G[b][a]=weigh;
}
cin>>k;
while(k--) {
vector<int> sq(N);
for(int i=0; i<n; i++) scanf("%d",&sq[i]);
int bg=sq[0];//起始点
Dijkstra(bg);//计算每个结点到达起始点的最短距离
int minV=INF,maxV=-1;
bool t1=true,t2=true;
for(int i=0; i<n; i++) {//判断当前结点距离起点的距离是否比之前结点都长
if(d[sq[i]]<maxV) {
t1=false;
break;
} else maxV=d[sq[i]];
}
// 这一个循环可以省略
for(int i=n-1; i>=0; i--) {//判断当前结点距离起点的距离是否比后面的结点都短
if(d[sq[i]]>minV) {
t2=false;
break;
} else minV=d[sq[i]];
}
if(!t1||!t2) printf("No\n");
else printf("Yes\n");
}
return 0;
}