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 ...
随机推荐
- vim的使用技巧--模式入门
vim作为编辑器之神,一直都是程序爱好者的最爱,与一般的编辑器的最大不同就是对模式的把握更加的细腻和得当.普通编辑主要分为使用菜单和使用键盘,菜单就是输入命令作用,键盘主要用来输入文本,中间穿插着使用 ...
- iOS关于本地推送
不多说 直接上代码 NSDate *now = [NSDate date]; UILocalNotification *reminderNotification = [[UILocalNoti ...
- pyinstaller打包exe程序各种坑!!!
pyinstaller打包python成exe可执行程序,各种报错,各种坑,在次记录下 一.pyinstaller打包报错for real_module_name, six_moduleAttribu ...
- JS和jquery加载的区别
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- LeetCode OJ-- Search a 2D Matrix
https://oj.leetcode.com/problems/search-a-2d-matrix/ 具有数据递增性质的一个二维数组,对它进行二分搜索. 首先搜索target所在的行,再找列. c ...
- C++ 找不到方法标识符
其实原因是这个CPP并没有面向对象的结构. 所以进行编译时是“顺序编译”的,而main函数的定义又在A的定义之前.自然找不到标识符了.
- 玲珑杯 Round #5 Problem E Tetration (枚举 + 欧拉公式)
题目链接 Tetration 题意 给定一个排列 现在可以任意调整这个排列的顺序 求$a_{1}^{a_{2}^{a_{3}^{...^{a_{n}}}}}$对$p$取模的最小值 直接枚举$a$ ...
- 伪全栈工程师做的有点简陋的ui设计
站酷:http://www.zcool.com.cn/work/ZMjEwMDIxMDA=.html 这个app 叫自我时间管理 是一个 工具 管理自己开会 购物 健身 记账等 的提醒与管理,还可 ...
- NOIP2016模拟赛三 Problem C: 不虚就是要AK
题目大意 给定一棵带有边权的树, 问你在树上随机选两个点, 它们最短路径上的边权之和为\(4\)的倍数的概率为多少. Solution 树分治. 没什么好讲的. #include <cstdio ...
- 我的CSS初始化,reset.css
* { margin:; padding:; text-decoration: none; -webkit-overflow-scrolling: touch !important; /*iOS惯性滚 ...