programming review (c++): (3)graph, binary search
I.graph
#include <iostream>
#include <vector> using namespace std;
vector<vector<int>> graph={{-,,,,},{,-,,,},{,,-,,},{,,,-,},{,,,,-}}; vector<int> toposort(vector<vector<int>> graph){
vector<int> res;
auto n=graph.size(),k=n;
vector<int> rudu(n,);
vector<int> visited(n,);
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(graph[i][j]>)
rudu[j]++;
}
}
while (k--) {
for(int i=;i<n;i++){
if(rudu[i]==&&visited[i]==){
res.push_back(i);
visited[i]=;
for(int j=;j<n;j++){
if(graph[i][j]>)
rudu[j]--;
}
break;
}
}
}
return res;
} vector<int> dijkstra(vector<vector<int>> graph,int t){
auto n=graph.size(),k=n;
vector<int> dist(n,INT32_MAX);
vector<int> visited(n,);
dist[t]=;
visited[t]=;
int cur=INT32_MAX;
while(k--){
cur=INT32_MAX;
for(int i=;i<n;i++){
if(visited[i]== && dist[i]<cur){
cur=dist[i];
t=i;
}
}
for(int i=;i<n;i++){
if(visited[i]== && graph[t][i]>){
dist[i]=min(dist[i],graph[t][i]+dist[t]);
}
}
visited[t]=;
}
return dist;
} int main(){
//1.DFS,BFS 类似树的非递归DFS和BFS,区别是需要一个list来标记哪些节点访问过,用来避免回路。 //2.拓扑排序(有向图),出度入度逐渐剪枝思想,可以用来检查图是否有环(无向图总是减去度为1的),检查是否完全联通或有几个联通子图
vector<int> topolist=toposort(graph); //3.最短路径 Dijkstra 该算法要求图中不存在负权边
vector<int> dist=dijkstra(graph,); }
II.binary search
重在边界上:
- begin=mid; vs begin=mid+1;
- end=mid; vs end=mid-1;
- if(begin<end) vs if(begin<=end)
- mid=(begin+end)/2 vs mid=(begin+end+1)/2
programming review (c++): (3)graph, binary search的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Unique Binary Search Trees [LeetCode]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 关于binary search的一点解惑
在写binary search时对于mid的计算我最开始使用的是 mid = (low + high)/2; 后来看到在很多的实现为 mid = low + (high - low)/2; 想了一下两 ...
- leetcode:Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode】96 - Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- 【转载】SQL SERVER 函数大全
SQL Server 函数大全 一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下 ...
- 深度学习_2_CNN
Basic Conception: 感受野(Reception Field) 权值共享(shared weights) 池化,即降采样(sub-Sampling) 卷积核(kernel,filter) ...
- [Machine Learning with Python] Data Preparation by Pandas and Scikit-Learn
In this article, we dicuss some main steps in data preparation. Drop Labels Firstly, we drop labels ...
- Go语言调度器之主动调度(20)
本文是<Go语言调度器源代码情景分析>系列的第20篇,也是第五章<主动调度>的第1小节. Goroutine的主动调度是指当前正在运行的goroutine通过直接调用runti ...
- Java爬虫系列之实战:爬取酷狗音乐网 TOP500 的歌曲(附源码)
在前面分享的两篇随笔中分别介绍了HttpClient和Jsoup以及简单的代码案例: Java爬虫系列二:使用HttpClient抓取页面HTML Java爬虫系列三:使用Jsoup解析HTML 今天 ...
- SDOI 2015 约束个数和
Description: 共\(T \le 5 \times 10^4\)组询问, 每组询问给定\(n\)和\(m\), 请你求出 \[ \sum_{i = 1}^n \sum_{j = 1}^m \ ...
- Laravel 时间处理
$info['date'] = $item->created_at->diffForHumans();//友好时间显示 $info['date'] = $item->created_ ...
- PHP利用lua实现Redis Sorted set的zPop操作
function zPop($key) { $script = <<<EOD local v = redis.call('zrange', KEYS[1], 0, 0); if v[ ...
- UIView和UIImageView 旋转消除锯齿方法
方法一: calendarImageView_ =[[UIImageView alloc] initWithFrame:CGRectMake(3,3,60,72)]; calendarImag ...
- 读取配置文件包含properties和xml文件
读取properties配置文件 /** * 读取配置文件 * @author ll-t150 */ public class Utils { private static Properties pr ...