当前位置: 首页 > news >正文

PAT 1163 Dijkstra Sequence

个人学习记录,代码难免不尽人意。

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 Nv​ .Then N elines 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<cstdio> 
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
int G[maxn][maxn];
int d[maxn];
bool visit[maxn];
bool dijkstra(int list[],int n){fill(d,d+maxn,INF);fill(visit,visit+maxn,false);d[list[0]]=0;int count=0;for(int i=0;i<n;i++){int m=-1,min=INF;for(int j=1;j<=n;j++){if(d[j]<min&&!visit[j]){m=j;min=d[j];}}if(d[m]!=d[list[count]]){return false;}else count++;visit[m]=true;for(int j=1;j<=n;j++){if(!visit[j]&&G[m][j]!=INF&&d[j]>d[m]+G[m][j]){d[j]=d[m]+G[m][j];}}}return true;
}int main(){for(int i=0;i<maxn;i++){for(int j=0;j<maxn;j++){G[i][j]=INF;}}int n,m;scanf("%d %d",&n,&m);for(int i=0;i<m;i++){int a,b,dis;scanf("%d %d %d",&a,&b,&dis);G[a][b]=dis;G[b][a]=dis;}int k;scanf("%d",&k);for(int i=0;i<k;i++){int list[n];for(int j=0;j<n;j++){scanf("%d",&list[j]);}if(dijkstra(list,n)){printf("Yes\n");}else{printf("No\n");}} 
}

这道题我一开始的做法是先正常用dijkstra算法求出d数组,然后再用给的序列求出另外一个d数组比较两个数组是否一致,如果一致输出yes,不一样输出no。**但是测试点1,3报错!证明不能这样做。后来我想了很久终于想明白了,哪怕是不按照dijkstra序列顺序,最终得到的结果也有可能相同!**这道题最简单的做法还是在dijkstra算法中判断当前处理节点和序列对应节点的距离源点的距离是否一致,如果不一致的话就可以判断不是dijkstra序列!因为可以肯定不是最小距离,一定不会在这个位置出现!

这道题让我知道,如果做题的时候部分正确,自己又肯定算法本身没有问题,肯定是出发点搞错了!可以换个方向重新写!

http://www.lryc.cn/news/152231.html

相关文章:

  • 嵌入式学习之进程
  • C#-单例模式
  • WSNs 安全技术
  • H5如何做页面下拉刷新和上拉加载
  • Camunda 7.x 系列【42】事件子流程
  • JVM类的加载过程
  • Jmeter如何设置中文版
  • flutter自定义按钮-文本按钮
  • 无涯教程-Android - CheckBox函数
  • [Go版]算法通关村第十五关青铜——用4KB内存寻找重复元素
  • OJ练习第159题——消灭怪物的最大数量
  • Prometheus-Rules(规则)
  • 打卡智能中国(六):村里出了“飞行员”
  • 自动化运维工具Ansible之playbooks剧本
  • K8S访问控制------认证(authentication )、授权(authorization )、准入控制(admission control )体系
  • 开开心心带你学习MySQL数据库之第三篇上
  • Mysql批量插入大量数据的方法
  • centos安装nginx实操记录(加安全配置)
  • 【中等】49. 字母异位词分组
  • Python3 条件控制
  • IDEA自定义模板
  • 【Unity3D】UI Toolkit简介
  • QT 界面相关操作
  • nestjs:docker build时执行npm install sharp提示downloading libvips socket hang up
  • 图像分类学习笔记(七)——MobileNet
  • ssm+vue宠物领养系统源码和论文
  • 阜时科技联合客户发布全固态激光雷达面阵SPAD芯片及雷达整机
  • leetcode 189. 轮转数组
  • 亚马逊广告收入突破百亿美元,有望成为下一个收入支柱来源?
  • MATLAB中isequal函数转化为C语言