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 Nv(≤10^3
​​ ) and Ne(≤10^5), 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 sequence是指这样一个攻占顺序能求得起点到达所有点的最短路径
Dijkstra sequence地杰斯特拉算法的过程是
    当前未攻占过的顶点中,距离最短的点为最佳攻占点(若有多个点选其中一点即可)
    然后攻占n次把所有城市都遍历到,走过的这一条路径即为最短路径
    最短路径可能有多条(若有多个最佳攻占点就会分支为多条路)
  这题就是判断输入的路径是否为dijkstra的最短路径

思路

和原来的Dijkstra的步骤相仿,但是每次选择攻占的城市都为既定的序列中的顶点u=path[i]

//让下一个攻占的城市假设就为预设的path[i]
//若有比path[i]路径更短的,说明path[i]不是最佳攻占点,那么这个就不是dijsktra序列

//是找攻占城市的过程,那不如假设就是序列中对应的城市,看它是否满足最佳攻占条件
int u=path[i],MIN=d[path[i]];
for(int j=1; j<=n; j++)
 if(!vis[j]&&d[j]<MIN)
  return false;

bool Dijkstra(int s) {
    fill(vis,vis+n,false);
    fill(d,d+n,INF);
    d[s]=0;
    for(int i=0; i<n; i++) {
        //让下一个攻占的城市假设就为预设的path[i]
        //若有比path[i]路径更短的,说明path[i]不是最佳攻占点,那么这个就不是dijsktra序列

        //是找攻占城市的过程,那不如假设就是序列中对应的城市,看它是否满足最佳攻占条件
        int u=path[i],MIN=d[path[i]];
        for(int j=1; j<=n; j++)
            if(!vis[j]&&d[j]<MIN)
                return false;

        vis[u]=true;
        for(map<int,int>::iterator it=mp[u].begin(); it!=mp[u].end(); it++) {
            int v=it->first,dis=it->second;
            if(!vis[v]&&d[u]+dis<d[v]) {
                d[v]=d[u]+dis;
            }
        }
    }
    return true;
}

AC代码

#include <bits/stdc++.h>
using namespace std;
const int maxv=1010;
const int INF=1e9;
map<int,int>mp[maxv];
int n,m,k,path[maxv],d[maxv];
bool vis[maxv];

bool Dijkstra(int s) {
    fill(vis,vis+n,false);
    fill(d,d+n,INF);
    d[s]=0;
    for(int i=0; i<n; i++) {
        //让下一个攻占的城市假设就为预设的path[i]
        //若有比path[i]路径更短的,说明path[i]不是最佳攻占点,那么这个就不是dijsktra序列

        //是找攻占城市的过程,那不如假设就是序列中对应的城市,看它是否满足最佳攻占条件
        int u=path[i],MIN=d[path[i]];
        for(int j=1; j<=n; j++)
            if(!vis[j]&&d[j]<MIN)
                return false;

        vis[u]=true;
        for(map<int,int>::iterator it=mp[u].begin(); it!=mp[u].end(); it++) {
            int v=it->first,dis=it->second;
            if(!vis[v]&&d[u]+dis<d[v]) {
                d[v]=d[u]+dis;
            }
        }
    }
    return true;
}
int main() {
    scanf("%d %d",&n,&m);
    int x,y,z;
    for(int i=0; i<m; i++) {
        scanf("%d %d %d",&x,&y,&z);
        mp[x][y]=mp[y][x]=z;
    }
    scanf("%d",&k);
    for(int i=0; i<k; i++) {
        for(int j=0; j<n; j++)
            scanf("%d",&path[j]);
        if(Dijkstra(path[0]))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

别人的写法

  在寻找最佳点时,保留所有最佳点,若当前选择的攻占点在最佳点序列中则符合,接着选择该点作为攻占点优化;若不在当前序列中,则不符合退出

void check() {
fill(d + 1, d + 1 + n, INF);
fill(vis + 1, vis + 1 + n, false);
d[now[0]] = 0;
set tmp;
int index = 0;
for (int i = 0; i < n; i++) {
int u, mini = INF;
for (int j = 1; j <= n; j++)
if (vis[j] == false) {
   if (d[j] < mini) {//更新最佳点
       mini = d[j];
       tmp.clear();
       tmp.insert(j);
   } else if (d[j] == mini)//同为最佳点,添加
     tmp.insert(j);
}

if (tmp.find(now[index]) != tmp.end()) {//不在当前序列中
vis[now[index]] = true;
u = now[index];
tmp.clear();
} else {
cout << “No” << endl;
return;
}
for (int j = 1; j <= n; j++)
if (vis[j] == false && matrx[u][j] != 0 && d[j] > d[u] + matrx[u][j])
d[j] = d[u] + matrx[u][j];
index++;
}
cout << “Yes” << endl;
}

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <math.h>
#define maxsize 1002
#define INF 99999999
using namespace std;
int n, m, matrx[maxsize][maxsize] = {0}, d[maxsize];
bool vis[maxsize] = {false};
vector<int> now;
void check() {
    fill(d + 1, d + 1 + n, INF);
    fill(vis + 1, vis + 1 + n, false);
    d[now[0]] = 0;
    set<int> tmp;
    int index = 0;
    for (int i = 0; i < n; i++) {
        int u, mini = INF;
        for (int j = 1; j <= n; j++)
            if (vis[j] == false) {
                if (d[j] < mini) {
                    mini = d[j];
                    tmp.clear();
                    tmp.insert(j);
                } else if (d[j] == mini)
                    tmp.insert(j);
            }
        if (tmp.find(now[index]) != tmp.end()) {
            vis[now[index]] = true;
            u = now[index];
            tmp.clear();
        } else {
            cout << "No" << endl;
            return;
        }
        for (int j = 1; j <= n; j++)
            if (vis[j] == false && matrx[u][j] != 0 && d[j] > d[u] + matrx[u][j])
                d[j] = d[u] + matrx[u][j];
        index++;
    }
    cout << "Yes" << endl;
}
int main() {
    std::iostream::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> n >> m;
    int a, b, c;
    for (int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        matrx[a][b] = matrx[b][a] = c;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        now.clear();
        for (int j = 0; j < n; j++) {
            cin >> a;
            now.push_back(a);
        }
        check();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deosiree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值