PAT(甲级)2019年秋季考试7-4 Dijkstra Sequence (30 分)

43 篇文章 0 订阅

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 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

就根据每次输入的序列的第一个数据进行Dijkstra算法,保存每次j进入set的最短路径的节点,然后根据得到的节点序列与给定的节点序列经行比较,如果同下标的两点的最短路径长度相同则继续判断,所有节点最短路径长度都相同则返回Yes,否则返回No

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int N, E, K, Dist[1005], e[1005][1005];
bool visit[1005];
vector<int> ans;
int main() {
	scanf("%d %d", &N, &E);
	int a, b, c;
	for (int i = 0; i < E; i++) {
		scanf("%d %d %d", &a, &b, &c);
		e[a][b] = e[b][a] = c;
	}
	scanf("%d", &K);
	for (int i = 0; i < K; i++) {
		vector<int> temp(N);
		for (int j = 0; j < N; j++) {
			scanf("%d", &temp[j]);
		}
		fill(Dist, Dist + 1005, INF);
		fill(visit, visit + 1005, false);
		Dist[temp[0]] = 0;
		ans.clear();
		int flag = 0;
		for (int j = 0; j < N; j++) {
			int minV = INF, V = -1;
			for (int t = 1; t <= N; t++) {
				if (!visit[t] && Dist[t] < minV) {
					minV = Dist[t];
					V = t;
				}
			}
			if (V == -1) break;
			visit[V] = true;
			ans.push_back(V);
			for (int t = 1; t <= N; t++) {
				if (!visit[t] && e[V][t] > 0) {
					if (Dist[t] > Dist[V] + e[V][t]) {
						Dist[t] = Dist[V] + e[V][t];
					}
				}
			}
		}
		for (int i = 0; i < N; i++) {
			if (Dist[temp[i]] != Dist[ans[i]]) {
				flag = 1;
				break;
			}
		}
		if (flag) printf("No\n");
		else printf("Yes\n");
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值