programming review (c++): (3)graph, binary search

I.graph

#include <iostream>
#include <vector>

using namespace std;
vector<vector<int>> graph={{-1,0,3,0,0},{2,-1,4,0,0},{0,0,-1,2,7},{0,0,0,-1,3},{0,0,0,0,-1}};

vector<int> toposort(vector<vector<int>> graph){
    vector<int> res;
    auto n=graph.size(),k=n;
    vector<int> rudu(n,0);
    vector<int> visited(n,0);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(graph[i][j]>0)
                rudu[j]++;
        }
    }
    while (k--) {
        for(int i=0;i<n;i++){
            if(rudu[i]==0&&visited[i]==0){
                res.push_back(i);
                visited[i]=1;
                for(int j=0;j<n;j++){
                    if(graph[i][j]>0)
                        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,0);
    dist[t]=0;
    visited[t]=1;
    int cur=INT32_MAX;
    while(k--){
        cur=INT32_MAX;
        for(int i=0;i<n;i++){
            if(visited[i]==0 && dist[i]<cur){
                cur=dist[i];
                t=i;
            }
        }
        for(int i=0;i<n;i++){
            if(visited[i]==0 && graph[t][i]>0){
                dist[i]=min(dist[i],graph[t][i]+dist[t]);
            }
        }
        visited[t]=1;
    }
    return dist;
}

int main(){
    //1.DFS,BFS 类似树的非递归DFS和BFS,区别是需要一个list来标记哪些节点访问过,用来避免回路。

    //2.拓扑排序(有向图),出度入度逐渐剪枝思想,可以用来检查图是否有环(无向图总是减去度为1的),检查是否完全联通或有几个联通子图
    vector<int> topolist=toposort(graph);

    //3.最短路径 Dijkstra  该算法要求图中不存在负权边
    vector<int> dist=dijkstra(graph,1);

}

II.binary search

重在  begin=mid; end=mid; begin=mid+1; end=mid-1; if(begin<end)   if(begin<=end)的选择上。

时间: 2024-12-23 06:03:49

programming review (c++): (3)graph, binary search的相关文章

[LeetCode] Unique Binary Search Trees II dfs 深度搜索

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}&

leetcode_96题——Unique Binary Search Trees(动态规划)

Unique Binary Search Trees Total Accepted: 48675 Total Submissions: 134436My Submissions Question Solution Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 uniq

96. Unique Binary Search Trees &amp;&amp; 95. Unique Binary Search Trees II &amp;&amp; 241. Different Ways to Add Parentheses

96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Program

【LeetCode】96 - Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Programming Solution 1:  递归 1 class So

leetcode || 96、Unique Binary Search Trees

problem: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Hide Tags Tree Dynamic Programming 题意:大小为

[Coding Made Simple] Optimal Binary Search Tree

Given keys and frequency at which these keys are searched, how would you create a binary search tree from these keys such that the cost of searching is minimum. The cost of searching is defined as the sum of all node's search frequency * its depth; R

LeetCode: Unique Binary Search Trees II 解题报告

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1         3     3      2      1 \      

leetcode || 95、Unique Binary Search Trees II

problem: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "

2018.3.5-6 knapsack problem, sequence alignment and optimal binary search trees

这周继续dynamic programming,这三个算法都是dynamic programming的. knapsack problem有一种greedy的解法,虽然简单但是不保证正确,这里光头哥讲的是dynamic的解法.其实和上次那个max weight independent set的算法差不多,同样是每个物件都判断一遍有这个物件和没这个物件两种情况,用bottom-up的方法来解,然后得到一个最大的value值,这时因为没有得到具体的选择方案,所以最后还需要一部重构的步骤得到具体方案.