Programming Review (C + +): (3) Graph, binary search

Source: Internet
Author: User

I.graph

#include <iostream>#include<vector>using namespaceStd;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(intI=0; i<n;i++){         for(intj=0; j<n;j++){            if(graph[i][j]>0) Rudu[j]++; }    }     while(k--) {         for(intI=0; i<n;i++){            if(rudu[i]==0&&visited[i]==0) {res.push_back (i); Visited[i]=1;  for(intj=0; j<n;j++){                    if(graph[i][j]>0) Rudu[j]--; }                 Break; }        }    }    returnRes;} Vector<int> Dijkstra (vector<vector<int>> Graph,intt) {Auto N=graph.size (), k=N; Vector<int>Dist (N,int32_max); Vector<int> Visited (n,0); Dist[t]=0; Visited[t]=1; intCur=Int32_max;  while(k--) {cur=Int32_max;  for(intI=0; i<n;i++){            if(visited[i]==0&& dist[i]<cur) {cur=Dist[i]; T=i; }        }         for(intI=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; }    returnDist;}intMain () {//1.dfs,bfs A tree-like non-recursive DFS and BFS, the difference is that a list is required to flag which nodes have been accessed to avoid loops. //2. Topological sequencing (direction graph), the degree of gradual pruning of the idea, can be used to check whether the graph has a ring (the graph is always minus the degree of 1), check whether it is fully connected or there are several Unicom sub-mapvector<int> topolist=Toposort (graph); //3. Shortest path Dijkstra The algorithm requires that no negative weights exist in the graph.vector<int> Dist=dijkstra (graph,1);}

Ii.binary Search

Heavy in the Begin=mid; End=mid; begin=mid+1; End=mid-1; if (begin<end) if (begin<=end) is selected.

Programming Review (C + +): (3) Graph, binary search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.