栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

A1163 Dijkstra Sequence(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【Dijkstra】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

A1163 Dijkstra Sequence(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【Dijkstra】

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 Nv​ (≤103) and Ne​ (≤105), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv​.

Then 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 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
代码如下:
#include
#include
#include
using namespace std;
const int MAXN=1010;
const int INF=0x3fffffff;
int map[MAXN][MAXN],d[MAXN];
bool visit[MAXN];
int nv,ne,k;
int main()
{
    cin>>nv>>ne;
    fill(map[0],map[0]+MAXN*MAXN,INF);
    for(int i=1;i<=ne;i++)
    {
        int v1,v2,w;
        cin>>v1>>v2>>w;
        map[v1][v2]=map[v2][v1]=w;
    }
    cin>>k;
    for(int i=0;i v(nv);
        for(int j=0;j>v[j];
        bool flag=true;
        fill(visit,visit+MAXN,false);
        fill(d,d+MAXN,INF);//d存的是每个点到起始点的距离
        d[v[0]]=0;//自己到自己为0
        for(int j=0;j 
运行结果如下: 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943067.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号