给定具有N个顶点和M个边以及一个边(U,V)的有向加权图。任务是查找是否存在从U到V的替代路径,替代路径中边的单个权重小于直接路径的权重。如果存在,则打印是,否则,打印否。
例子
For the given directed graph:
Input: N = 7, M = 10, U = 3, V = 6.
Output: No
Explanation:
For the given edge {3, 6}, weight = 16. There is no alternate path to reach 6 from 3. Hence the answer is No.
Input: N = 7, M = 10, U = 1, V = 6.
Output: Yes
Explanation:
=> For the given edge {1, 6}, weight = 5.
=> Alternate path to reach 6 from 1 = {1, 5, 6} with individual weights {12, 2} which is more than 5. Hence this path cannot be considered.
=> Alternate path to reach 6 from 1 = {1, 2, 4, 5, 6} with individual weights {5, 1, 1, 2} which is not less than 5. Hence this path cannot be considered.
=> Alternate path to reach 6 from 1 = {1, 4, 5, 6} with individual weights {3, 1, 2} which is less than 5. Hence this path can be considered.
=> Hence the answer is Yes
方法:
- 使用深度优先搜索(DFS)遍历遍历给定的有向图和起始顶点U。
- 在DFS遍历期间,如果任何边的权重大于有向边的权重,则不包括该路径。
- 如果我们以遍历路径中每个边的权重小于有向边的顶点到达顶点V ,则存在替代路径。
- 否则,除了直接路径外,在顶点U和顶点V之间没有其他路径。
下面是上述方法的实现:
C++14
// C++ program for above approach
#include
using namespace std;
// Edge class
class Edge
{
public:
int u;
int v;
int w;
// Edge constructor to
// initialize edge (u, v) with
// weight w
Edge(int u, int v, int w)
{
this->u = u;
this->v = v;
this->w = w;
}
};
class GFG{
public:
// Array to mark already
// visited vertices
bool *visited;
// Adjacency list representation
// of the graph
vector *graph;
// GfG class constructor
GFG(int size)
{
visited = new bool[size];
graph = new vector[size];
}
// Depth First Search to traverse
// all vertices with weight less
// than weight of the dfs root
void dfs(int S, int W)
{
// Marking the vertex visited
visited[S] = true;
// Traversing adjacent vertex
for(Edge *uv : graph[S])
{
int ver = uv->v;
int w = uv->w;
if (!visited[ver] && w < W)
dfs(ver, W);
}
}
};
// Driver code
int main()
{
// Number of vertices
int N = 7;
// Number of edges
int M = 10;
// Edge to be checked
int U_V[] = {3, 6};
// Creating GfG object
GFG *obj = new GFG(8);
// Creating edges
Edge *e0 = new Edge(1, 2, 5);
Edge *e1 = new Edge(1, 4, 3);
Edge *e2 = new Edge(1, 5, 12);
Edge *e3 = new Edge(1, 6, 5);
Edge *e4 = new Edge(4, 5, 1);
Edge *e5 = new Edge(5, 6, 2);
Edge *e6 = new Edge(5, 3, 1);
Edge *e7 = new Edge(3, 6, 16);
Edge *e8 = new Edge(4, 7, 1);
Edge *e9 = new Edge(2, 4, 1);
// Adding edges to the graph
obj->graph[1].push_back(e0);
obj->graph[1].push_back(e1);
obj->graph[1].push_back(e2);
obj->graph[1].push_back(e3);
obj->graph[4].push_back(e4);
obj->graph[5].push_back(e5);
obj->graph[5].push_back(e6);
obj->graph[3].push_back(e7);
obj->graph[4].push_back(e8);
obj->graph[2].push_back(e9);
// DFS traversal from
// vertex U
obj->dfs(U_V[0], 16);
// If there is alternate
// path then print YES,
// else NO
if (obj->visited[U_V[1]])
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}
}
// This code is contributed by sanjeev2552
Java
// Java program for above approach
import java.util.*;
// To ignore the unchecked warning
@SuppressWarnings("unchecked")
// GfG class
public class GfG {
// Array to mark already
// visited vertices
static private boolean visited[];
// Adjacency list representation
// of the graph
static private ArrayList graph[];
// GfG class constructor
public GfG(int size)
{
visited = new boolean[size];
graph = new ArrayList[size];
}
// Edge class
static class Edge {
int u;
int v;
int w;
// Edge constructor to
// initialize edge (u, v) with
// weight w
Edge(int u, int v, int w)
{
this.u = u;
this.v = v;
this.w = w;
}
}
// Helper method to
// initialize graph
static private void helperInitialize(int size)
{
for (int i = 0; i < size; i++) {
graph[i] = new ArrayList();
}
}
// Depth First Search to traverse
// all vertices with weight less
// than weight of the dfs root
static private void dfs(int S, int W)
{
// Marking the vertex visited
visited[S] = true;
// Traversing adjacent vertex
for (Edge uv : graph[S]) {
int ver = uv.v;
int w = uv.w;
if (!visited[ver] && w < W)
dfs(ver, W);
}
}
// Driver function
public static void main(String[] args)
{
// Number of vertices
int N = 7;
// Number of edges
int M = 10;
// Edge to be checked
int U_V[] = { 3, 6 };
// Creating GfG object
GfG obj = new GfG(8);
// Initializing graph
helperInitialize(8);
// Creating edges
Edge e0 = new Edge(1, 2, 5);
Edge e1 = new Edge(1, 4, 3);
Edge e2 = new Edge(1, 5, 12);
Edge e3 = new Edge(1, 6, 5);
Edge e4 = new Edge(4, 5, 1);
Edge e5 = new Edge(5, 6, 2);
Edge e6 = new Edge(5, 3, 1);
Edge e7 = new Edge(3, 6, 16);
Edge e8 = new Edge(4, 7, 1);
Edge e9 = new Edge(2, 4, 1);
// Adding edges to the graph
graph[1].add(e0);
graph[1].add(e1);
graph[1].add(e2);
graph[1].add(e3);
graph[4].add(e4);
graph[5].add(e5);
graph[5].add(e6);
graph[3].add(e7);
graph[4].add(e8);
graph[2].add(e9);
// DFS traversal from
// vertex U
dfs(U_V[0], 16);
// If there is alternate
// path then print YES,
// else NO
if (visited[U_V[1]]) {
System.out.print("No");
}
else {
System.out.print("Yes");
}
}
}
Python3
# Python3 program for above approach
class Edge:
# Edge constructor to
# initialize edge (u, v) with
# weight w
def __init__(self, u, v, w):
self.u = u
self.v = v
self.w = w
# Depth First Search to traverse
# all vertices with weight less
# than weight of the dfs root
def dfs(S, W):
global visited,graph
# Marking the vertex visited
visited[S] = True
# Traversing adjacent vertex
for uv in graph[S]:
ver = uv.v
w = uv.w
if (not visited[ver] and w < W):
dfs(ver, W)
# Driver code
if __name__ == '__main__':
# Number of vertices
N = 7
# Number of edges
M = 10
# Edge to be checked
U_V = [3, 6]
# Creating GfG object
visited, graph = [False for i in range(8)], [[] for i in range(8)]
# Creating edges
e0 = Edge(1, 2, 5)
e1 = Edge(1, 4, 3)
e2 = Edge(1, 5, 12)
e3 = Edge(1, 6, 5)
e4 = Edge(4, 5, 1)
e5 = Edge(5, 6, 2)
e6 = Edge(5, 3, 1)
e7 = Edge(3, 6, 16)
e8 = Edge(4, 7, 1)
e9 = Edge(2, 4, 1)
# Adding edges to the graph
graph[1].append(e0)
graph[1].append(e1)
graph[1].append(e2)
graph[1].append(e3)
graph[4].append(e4)
graph[5].append(e5)
graph[5].append(e6)
graph[3].append(e7)
graph[4].append(e8)
graph[2].append(e9)
# DFS traversal from
# vertex U
dfs(U_V[0], 16)
# If there is alternate
# path then prYES,
# else NO
if (visited[U_V[1]]):
print("NO")
else :
print("YES")
# This code is contributed by mohit kumar 29
C#
// C# program for above approach
using System;
using System.Collections.Generic;
// GfG class
class GfG
{
// Array to mark already
// visited vertices
static private bool []visited;
// Adjacency list representation
// of the graph
static private List []graph;
// GfG class constructor
public GfG(int size)
{
visited = new bool[size];
graph = new List[size];
}
// Edge class
class Edge
{
public int u;
public int v;
public int w;
// Edge constructor to
// initialize edge (u, v) with
// weight w
public Edge(int u, int v, int w)
{
this.u = u;
this.v = v;
this.w = w;
}
}
// Helper method to
// initialize graph
static private void helperInitialize(int size)
{
for (int i = 0; i < size; i++)
{
graph[i] = new List();
}
}
// Depth First Search to traverse
// all vertices with weight less
// than weight of the dfs root
static private void dfs(int S, int W)
{
// Marking the vertex visited
visited[S] = true;
// Traversing adjacent vertex
foreach (Edge uv in graph[S])
{
int ver = uv.v;
int w = uv.w;
if (!visited[ver] && w < W)
dfs(ver, W);
}
}
// Driver function
public static void Main(String[] args)
{
// Edge to be checked
int []U_V = { 3, 6 };
// Creating GfG object
GfG obj = new GfG(8);
// Initializing graph
helperInitialize(8);
// Creating edges
Edge e0 = new Edge(1, 2, 5);
Edge e1 = new Edge(1, 4, 3);
Edge e2 = new Edge(1, 5, 12);
Edge e3 = new Edge(1, 6, 5);
Edge e4 = new Edge(4, 5, 1);
Edge e5 = new Edge(5, 6, 2);
Edge e6 = new Edge(5, 3, 1);
Edge e7 = new Edge(3, 6, 16);
Edge e8 = new Edge(4, 7, 1);
Edge e9 = new Edge(2, 4, 1);
// Adding edges to the graph
graph[1].Add(e0);
graph[1].Add(e1);
graph[1].Add(e2);
graph[1].Add(e3);
graph[4].Add(e4);
graph[5].Add(e5);
graph[5].Add(e6);
graph[3].Add(e7);
graph[4].Add(e8);
graph[2].Add(e9);
// DFS traversal from
// vertex U
dfs(U_V[0], 16);
// If there is alternate
// path then print YES,
// else NO
if (visited[U_V[1]])
{
Console.Write("No");
}
else
{
Console.Write("Yes");
}
}
}
// This code is contributed by 29AjayKumar
Yes
时间复杂度: O(N + M),其中N =顶点数,M =边数。