PTA 1163 Dijkstra Sequence (30 分)

1163 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 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

感悟

英文水平太烂,对着翻译都看的云里雾里,看半天才搞懂什么意思,就用dijkstra算法检查一下序列是否符合要求,符合输出Yes,不符合输出No,挺裸的一道题.主要是英文看的头疼...感觉IF饮食法不适合我这种晚上读书的人,一动脑子血糖就蹭蹭往下降,一会就贼饿,不吃脑子就动不了,得考虑考虑怎么调整一下饮食计划和学习计划,不然减脂也减不了,书也读不了.

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 1010, INF = 0x3f3f3f3f;
int g[N][N];
int a[N];
int n, m;
int check()
{
    int used[N] = {0},dist[N] = {0};
    memset(dist,0x3f,sizeof dist);
    dist[a[1]] = 0;
    for(int i = 1 ; i <= n; i ++)
    {
        int u = -1;
        for(int v = 1 ; v <= n ; v ++)
            if(!used[v] && (u == -1 || dist[u] > dist[v] )) u = v;
        if(dist[u] != dist[a[i]])
            return 0;
        used[u] = 1;
        for(int v = 1 ; v <= n; v ++)
            if(v == u) continue;
            else dist[v] = min(dist[v],dist[u]+g[u][v]);
    }
    return 1;
}

int main()
{
    
    scanf("%d %d", &n, &m);
    memset(g, 0x3f, sizeof(g));
    for(int i = 0 ; i < m ; i++)
    {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        g[a][b] = g[b][a] = c;
    }
    int k;
    scanf("%d",&k);
    for(int i = 0 ; i < k ; i++)
    {
        for(int j = 1 ;j <= n ; j++)
            scanf("%d", &a[j]);
        if(check()) printf("Yes\n");
        else printf("No\n");
    }

    return 0;
}
posted @ 2022-02-13 02:10  别问了我什么都不会  阅读(232)  评论(0编辑  收藏  举报