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 ...
随机推荐
- 打造好用的编辑终端环境yakuake
我喜欢使用vim,由于不喜欢不够纯粹的东西,所以将伪终端打造的和真终端几乎一样,我的yakuake全屏是真的全屏,你也想这样,跟我来吧. 一.修改yakuake的皮肤文件,在/usr/share/ya ...
- 在 Fedora 26/27 GNOME 3.24/3.26 环境中安装 FCITX 小企鹅输入法(修订)
之前我曾经写过一篇文章介绍在 GNOME 3.x 下安装小企鹅输入法框架,但最近在 Fedora 26/27 环境下发现老方法已经失效了,会导致 GNOME 3.24/3.26 桌面在重启后无法进入, ...
- Codeforces 899 C.Dividing the numbers-规律
C. Dividing the numbers time limit per test 1 second memory limit per test 256 megabytes input s ...
- [Machine Learning with Python] Cross Validation and Grid Search: An Example of KNN
Train model: from sklearn.model_selection import GridSearchCV param_grid = [ # try 6 (3×2) combinati ...
- Topcoder SRM 144 DIV 1
BinaryCode 模拟 题意是:定义串P,Q,其中Q[i]=P[i-1]+P[i]+P[i+1],边界取0,并且P必须是01串.现在给你Q,让你求出P. 做法是:枚举第一位是1还是0,然后就可以推 ...
- [SPOJ7001]VLATTICE - Visible Lattice Points
题目大意: $q(q\leq50)$组询问,对于给定的$n(n\leq10^7)$,求$\displaystyle\sum_{i=0}^n\sum_{j=0}^n\sum_{k=0}^n[\gcd(i ...
- UVA 11389 The Bus Driver Problem 贪心水题
题目链接:UVA - 11389 题意描述:有n个司机,n个早班路线和n个晚班路线,给每个司机安排一个早班路线和一个晚班路线,使得每个早班路线和晚班路线只属于一个司机.如果一个司机早班和晚班总的驾驶时 ...
- inux 下的/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用
/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取. ~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的sh ...
- 在delphi中,如何把十进制数转换为十六进制整形数。若用inttohex只能转化为十六进制字符串。
var b: Byte; s: string;begin s := '31'; //16进制字符串 b := StrToInt('$' + s);end; 不过要注意一点,如果在程序调试时想看b的值, ...
- String、Stringbuffer和Stringbuilder之间的区别
关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.在执行速度方面:Stringbuilder>Stringbuffer&g ...