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 
#include
#include
#include
#include
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序列!因为可以肯定不是最小距离,一定不会在这个位置出现!

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

你可能感兴趣的:(PTA,算法,c++,pat,数据结构)