2019年9月8日秋季PAT甲级题解-4 Dijkstra Sequence (30 分)

34 篇文章 0 订阅

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​​ (≤10​^3​​) and N​e​​ (≤10^​5​​), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N​v​​.

Then N​e​​ 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 N​v​​ 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

一开始就是想不通如果当前距离源点最近的点有好几个该怎么办,其实只要判断当前找出的这个点和给定序列的当前位的dis是否相同即可,如果不同,证明给定序列的这个点不是最近的点,所以直接false。如果相同,接下来将给定序列的点赋值给u,因为它们的距离都一样,而且用这个点对后序进行松弛不会产生误差。

//判断这条路径是否是一条dij路径,因为dij每一步都是离源点最近的且未访问的结点,但是有可能不止一个,注意结点是从1到n的 
//所以我们在找出u后,不是拿u直接和下一个结点比较,而是比较它们俩个的dis,如果相等,证明它们两个都是当前距离s最近的,下面将u换成下一个结点,让它来进行松弛
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1005;
const int INF = 1e9;
int G[maxn][maxn],dis[maxn],vis[maxn];

int main(){
	fill(G[0],G[0]+maxn*maxn,INF);
	int n,m;
	cin>>n>>m;
	int u,v,dist;
	for(int i = 0; i < m; i++){
		cin>>u>>v>>dist;
		G[u][v] = G[v][u] = dist;
	} 
	int q;
	cin>>q;
	for(int i = 0; i < q; i++){
		bool isDij = true;
		fill(dis,dis+maxn,INF);
		fill(vis,vis+maxn,false);
		vector<int> v(n+1);
		for(int j = 0;j < n; j++){
			cin>>v[j];
		}
		dis[v[0]] = 0;
		for(int j = 0; j < n; j++){
			int u = -1,minDis = INF;
			for(int l = 1; l <= n; l++){
				if(dis[l] < minDis && vis[l] == false){
					u = l;
					minDis = dis[l];
				}
			}
			if(u == -1 || dis[u] != dis[v[j]]){
				isDij = false;
				break;
			}
			u = v[j];//将第j个数赋给u,因为u不一定是这个数,但是它们的离源点的距离一定相等 
			vis[u] = true;
			for(int v = 1; v <= n; v++){//用u进行松弛,松弛它的连通结点 
				if(G[u][v] != INF && vis[v] == false){
					if(G[u][v] + dis[u] < dis[v]){
						dis[v] = G[u][v]+dis[u];
					}
				}
			}
		}
		if(!isDij) cout<<"No\n";
		else cout<<"Yes\n";
	}
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值