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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章