4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制Course Schedule 课程清单Course Schedule II 课程清单之二。目前看来CareerCup中有关图的题在第四章中仅此一道,这是一道关于有向图的题,书中是用java来做的,我们用c++来做时要定义图和节点,这里参考了之前那道Clone Graph 无向图的复制中关于无向图的定义,并且在里面加入了一个枚举类变量state,来帮助我们遍历。这种找两点之间路径的题就是遍历的问题,可以用BFS或DFS来解,先来看BFS的解法,如下所示:

//Definition for directed graph.
enum State {
Unvisited, Visited, Visiting
}; struct DirectedGraphNode {
int label;
State state;
vector<DirectedGraphNode *> neighbors;
DirectedGraphNode(int x) : label(x) {};
}; struct DirectedGraph {
vector<DirectedGraphNode*> nodes;
}; class Solution {
public:
bool search(DirectedGraph *g, DirectedGraphNode *start, DirectedGraphNode *end) {
queue<DirectedGraphNode*> q;
for (auto a : g->nodes) a->state = Unvisited;
start->state = Visiting;
q.push(start);
while (!q.empty()) {
DirectedGraphNode *node = q.front(); q.pop();
for (auto a : node->neighbors) {
if (a->state == Unvisited) {
if (a == end) return true;
else {
a->state = Visiting;
q.push(a);
}
}
}
node->state = Visited;
}
return false;
}
};

[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径的更多相关文章

  1. Lintcode: Route Between Two Nodes in Graph

    Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...

  2. Route Between Two Nodes in Graph

    Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...

  3. [Swift]LeetCode882. 细分图中的可到达结点 | Reachable Nodes In Subdivided Graph

    Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...

  4. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

    Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...

  5. [LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点

    Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...

  6. 882. Reachable Nodes In Subdivided Graph

    题目链接 https://leetcode.com/contest/weekly-contest-96/problems/reachable-nodes-in-subdivided-graph/ 解题 ...

  7. 关于app.js和route.js和service.js还有controller.js中的依赖关系

    2.只要是由路由去执行的的控制器模块,必须注入到app.js里面进行依赖,在页面上就不需要ng-controller在html页面上写了:   但是如果一个控制器模块,没有经过路由管理:那么就必须要, ...

  8. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  9. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. Gleeo Time Tracker简明使用教程

    转载一篇很不错的文章,这款软件还是非常实用的 1 简介 Gleeo Time Tracker是安卓平台下一款相当酷的项目时间记录和管理的软件.说他酷,是因为界面纯黑.而除了这点酷之外,功能也很简单实用 ...

  2. 关于Redis中的serverCron

    1.serverCron简介 在 Redis 中, 常规操作由 redis.c/serverCron 实现, 它主要执行以下操作 /* This is our timer interrupt, cal ...

  3. POI教程之第二讲:创建一个时间格式的单元格,处理不同内容格式的单元格,遍历工作簿的行和列并获取单元格内容,文本提取

    第二讲 1.创建一个时间格式的单元格 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet=wb.createSheet("第一个 ...

  4. 烂泥:centos6.4服务器添加新硬盘

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 公司FTP服务器的空间又不够了,唉,没有办法只能新加硬盘了.因为以前没有给Linux服务器添加过硬盘,所以只能先在虚拟机中进行模拟. 新加硬盘的操作步骤 ...

  5. Hive权限控制和超级管理员的实现

    Hive权限控制 Hive权限机制: Hive从0.10可以通过元数据控制权限.但是Hive的权限控制并不是完全安全的.基本的授权方案的目的是防止用户不小心做了不合适的事情. 先决条件: 为了使用Hi ...

  6. Jetson TK1刷机+配置Mini PCI-e无线网卡

    最近买了台4K电视,觉得可以当显示器用,但没主机,不知怎的想到了Jetson TK1,于是一冲动买了.因为没网线,而Jetson TK1没有无线网卡,所以也折腾了一番,记录一下,给万一也有像我一样没有 ...

  7. SPOJ AMR12A The Black Riders --二分+二分图最大匹配

    题意:有n个人,m个洞.每个洞能容纳一个人,每个人到每个洞需要花费一些时间.每个人到达一个洞后可以花C的时间来挖一个洞,并且最多挖一个洞,这样又能多容纳一人.求能使至少K个人进洞的最短时间. 解法:看 ...

  8. UVA 12382 Grid of Lamps --贪心+优先队列

    题意:给出每行每列至少有的灯泡数,问最少有的灯泡数. 解法:要使灯泡数尽量小,说明要使交叉点尽量多,这样即抵了行,又抵了列,为最优的.所以可以用行来消去列,也可以用列来消去行,我这里是列来消去行.首先 ...

  9. 2014 Super Training #1 C Ice-sugar Gourd 模拟,扫描线

    原题 HDU 3363 http://acm.hdu.edu.cn/showproblem.php?pid=3363 给你一个串,串中有H跟T两种字符,然后切任意刀,使得能把H跟T各自分为原来的一半. ...

  10. Redis漏洞?阿里云被攻击!

    今天运维那边过来说阿里云服务器进程被占用很多,后来查了一下进程发现了这个玩意: 小编我看不懂,经运维先森仔细研究,发现这是被注入进来的一个进程,服务器被当成了肉鸡,专门用来跑比特币的,这样对方就不需要 ...