2019 PAT甲级秋季考试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
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int ma[1005][1005],a[1005];
int dis[1005];
int vis[1005];
int main()
{
	int n,m,k,t,x,y,z,i,j;
	scanf("%d%d",&n,&m);
	memset(ma,INF,sizeof ma);
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		ma[x][y]=ma[y][x]=z;
	}
	for(k=1;k<=n;k++)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(ma[i][j]>ma[i][k]+ma[k][j])
				{
					ma[i][j]=ma[i][k]+ma[k][j];
				}
			}
		}
	}
	scanf("%d",&t);
	while(t--)
	{
		for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		int s=a[0],f=0;
		for(i=1;i<n;i++)
		{
			if(ma[s][a[i]]>ma[s][a[i+1]])
			{
				f=1;
				break;
			}
		}
		if(f)printf("No\n");
		else printf("Yes\n");
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Dijkstra算法是一种用于解决最短路径问题的贪心算法。该算法的基本思想是从起始节点开始,通过不断扩展当前已找到的最短路径来逐步确定最短路径的结果。 具体的实现步骤如下: 1. 初始化:设置起始节点为当前节点,将起始节点到自身的距离为0,将起始节点到其他节点的距离设为无穷大。 2. 判断是否遍历了所有节点:如果还有未处理的节点,则继续执行下述步骤。否则,算法结束。 3. 遍历邻接节点:对于当前节点的所有邻接节点,计算经过当前节点到达该邻接节点的距离。如果该距离小于已确定的最短距离,则更新最短距离。 4. 选择下一个节点:从未处理的节点中选取距离起始节点最近的节点作为下一个节点。 5. 将选择的节点标记为处理完成。 6. 跳转至步骤2。 通过以上步骤,Dijkstra算法可以得到从起始节点到图中所有其他节点的最短路径。在实际应用中,可以使用优先队列来高效地实现步骤4的节点选择操作。 然而,需要注意的是,Dijkstra算法对于存在负权边的图无法正确处理,因为它会假设经过已处理的节点的路径是最短路径。如果图中存在负权边,可以使用Bellman-Ford算法来解决。此外,Dijkstra算法的时间复杂度为O(V^2),其中V表示节点的个数。若要减少时间复杂度,可以使用堆优化的Dijkstra算法,其时间复杂度为O((V+E)logV),其中E表示边的个数。 总之,Dijkstra算法是一种解决最短路径问题的有效算法,通过不断扩展已找到的最短路径来逐步确定最短路径的结果。在实际应用中,可以根据具体情况选择不同的优化策略。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值