给定一个未加权的图形和一个布尔阵列A [],其中如果数组A []表示的第i个指数如果该节点可以可以被访问(0)或不(1)。任务是找到从第0个节点到达第(N – 1)个节点的最短路径。如果无法到达,则打印-1 。
例子 :
Input : N = 5, A[] = {0, 1, 0, 0, 0}, Edges = {{0, 1}, {0, 2}, {1, 4}, {2, 3}, {3, 4}}
Output: 3
Explanation: There are two paths from 0th house to 4th house
- 0 → 1 → 4
- 0 →2 → 3 → 4
Since a policeman is present at the 1st house, the only path that can be chosen is the 2nd path.
Input : N = 4, A = {0, 1, 1, 0}, Edges = {{0, 1}, {0, 2}, {1, 3}, {2, 3}}
Output : -1
方法:这个问题类似于在未加权图中寻找最短路径。因此,可以使用 BFS 解决该问题。
请按照以下步骤解决问题:
- 初始化一个 unordered_map,比如说adj来存储边。如果在节点a或节点b有警察,则必须排除边(a, b) 。
- 初始化一个变量, pathLength = 0。
- 初始化一个布尔数据类型的向量,比如visited ,来存储一个节点是否被访问过。
- 初始化一个数组dist[0, 1, …., v-1] 使得dist[i]存储顶点i到根顶点的距离
- 初始化一个队列并将节点0推入其中。此外,将节点0标记为已访问。
- 在队列不为空且节点N-1未被访问的情况下进行迭代,从队列中弹出前元素,将队列前元素有边且未被访问的所有元素压入队列并增加所有这些节点的距离为1 + dist[q.top()] 。
- 如果节点(N – 1)未被访问,则打印-1 。
- 否则,打印第(N – 1)个节点到根节点的距离。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to create graph edges
// where node A and B can be visited
void createGraph(unordered_map >& adj,
int paths[][2], int A[], int N, int E)
{
// Visit all the connections
for (int i = 0; i < E; i++) {
// If a policeman is at any point of
// connection, leave that connection.
// Insert the connect otherwise.
if (!A[paths[i][0]] && !A[paths[i][1]]) {
adj[paths[i][0]].push_back(paths[i][1]);
}
}
}
// Function to find the shortest path
int minPath(int paths[][2], int A[],
int N, int E)
{
// If police is at either at
// the 1-st house or at N-th house
if (A[0] == 1 || A[N - 1] == 1)
// The thief cannot reach
// the N-th house
return -1;
// Stores Edges of graph
unordered_map > adj;
// Function call to store connections
createGraph(adj, paths, A, N, E);
// Stores wheather node is
// visited or not
vector visited(N, 0);
// Stores distances
// from the root node
int dist[N];
dist[0] = 0;
queue q;
q.push(0);
visited[0] = 1;
// Visit all nodes that are
// currently in the queue
while (!q.empty()) {
int temp = q.front();
q.pop();
for (auto x : adj[temp]) {
// If current node is
// not visited already
if (!visited[x]) {
q.push(x);
visited[x] = 1;
dist[x] = dist[temp] + 1;
}
}
}
if (!visited[N - 1])
return -1;
else
return dist[N - 1];
}
// Driver Code
int main()
{
// N : Number of houses
// E: Number of edges
int N = 5, E = 5;
// Given positions
int A[] = { 0, 1, 0, 0, 0 };
// Given Paths
int paths[][2] = { { 0, 1 },
{ 0, 2 },
{ 1, 4 },
{ 2, 3 },
{ 3, 4 } };
// Function call
cout << minPath(paths, A, N, E);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to create graph edges
// where node A and B can be visited
static void
createGraph(HashMap > adj,
int paths[][], int A[], int N, int E)
{
// Visit all the connections
for (int i = 0; i < E; i++) {
// If a policeman is at any point of
// connection, leave that connection.
// Insert the connect otherwise.
if (A[paths[i][0]] != 1
&& A[paths[i][1]] != 1) {
ArrayList list = adj.getOrDefault(
paths[i][0], new ArrayList<>());
list.add(paths[i][1]);
adj.put(paths[i][0], list);
}
}
}
// Function to find the shortest path
static int minPath(int paths[][], int A[], int N, int E)
{
// If police is at either at
// the 1-st house or at N-th house
if (A[0] == 1 || A[N - 1] == 1)
// The thief cannot reach
// the N-th house
return -1;
// Stores Edges of graph
HashMap > adj
= new HashMap<>();
// Function call to store connections
createGraph(adj, paths, A, N, E);
// Stores wheather node is
// visited or not
boolean visited[] = new boolean[N];
// Stores distances
// from the root node
int dist[] = new int[N];
dist[0] = 0;
ArrayDeque q = new ArrayDeque<>();
q.addLast(0);
visited[0] = true;
// Visit all nodes that are
// currently in the queue
while (!q.isEmpty()) {
int temp = q.removeFirst();
for (int x : adj.getOrDefault(
temp, new ArrayList<>())) {
// If current node is
// not visited already
if (!visited[x]) {
q.addLast(x);
visited[x] = true;
dist[x] = dist[temp] + 1;
}
}
}
if (!visited[N - 1])
return -1;
else
return dist[N - 1];
}
// Driver Code
public static void main(String[] args)
{
// N : Number of houses
// E: Number of edges
int N = 5, E = 5;
// Given positions
int A[] = { 0, 1, 0, 0, 0 };
// Given Paths
int paths[][] = {
{ 0, 1 }, { 0, 2 }, { 1, 4 }, { 2, 3 }, { 3, 4 }
};
// Function call
System.out.print(minPath(paths, A, N, E));
}
}
// This code is contributed by Kingash.
输出:
3
时间复杂度: O (N + E)
辅助空间: O(N+E)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。