1163 Dijkstra Sequence (30 point(s)) PAT

1163 Dijkstra Sequence (30 point(s))

题目

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

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

思路

题意

给出一个包含Nv个结点和Ne条边的图,给出k组测试数据,每组测试数据按一定顺序给出Nv个结点,判断这Nv个结点的顺序是否符合Dijkstra算法。

思路

用Dijkstra的标准算法,在每次得出当前的最小距离结点时,比较这个距离是否与当前给出的结点的序列最小相等距离。

解法

#include<bits/stdc++.h>
using namespace std;

int nv, ne, k;
int weight[1001005];

bool quiry() {
	int visited[1005] = { 0 }, dist[1005], tmp[1005];
	for (int i = 1; i <= nv; i++)
		scanf("%d", &tmp[i]);
	memset(dist, 0x3f, sizeof(dist));
	dist[tmp[1]] = 0;
	for (int i = 1; i <= nv; i++) {
		int node = -1;
		for (int j = 1; j <= nv; j++) {
			if (visited[j] == 0 && (node == -1 || dist[node] > dist[j]))
				node = j;
		}//找到最小距离的点
		if (dist[node] != dist[tmp[i]])
			return false;
		visited[node] = 1;//写tmp[i]的话下面的node都要变
		for (int j = 1; j <= nv; j++) {//更新所有结点的距离
			if (visited[j] == 0)
				dist[j] = min(dist[j], dist[node] + weight[node * 1000 + j]);
		}

	}
	return true;
}

int main() {
	scanf("%d%d", &nv, &ne);
	memset(weight, 0x3f, sizeof(weight));
	for (int i = 0; i < ne; i++) {
		int a, b, w;
		scanf("%d%d%d", &a, &b, &w);
		weight[a * 1000 + b] = weight[b * 1000 + a] = w;
	}
	scanf("%d", &k);
	for (int i = 0; i < k; i++) {
		if (quiry()) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

注意

  • 严格按照Dijkstra的标准算法来写,不要随便跳步骤
  • 魔改传统算法的时候注意新添加的变量和传统算法中对应变量是否一致
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值