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