#include"iostream"#include"vector"#include"queue"#include<cstring>usingnamespace std;constint MAXN =1e5;
vector<int> edge[MAXN];
vector<int> DFSGraph, BFSGraph;int vis[MAXN];voidDFS(constint&x){
vis[x]=1;for(auto cur : edge[x]){if(!vis[cur]){
DFSGraph.push_back(cur);DFS(cur);}}}voidBFS(constint&x){
queue <int> q;
q.push(x);
vis[x]=1;while(!q.empty()){int cur = q.front();
q.pop();
BFSGraph.push_back(cur);for(auto i : edge[cur]){if(!vis[i]){
vis[i]=1;
q.push(i);}}}}// topolpgicalSort with BFS
vector<int>topolpgicalSort(constint& vertices,const vector<vector<int>>& edges){
vector<vector<int>>graph(vertices, vector<int>());
vector<int>indegree(vertices,0), res;for(constauto& cur : edges){
graph[cur[1]].push_back(cur[0]);
indegree[cur[0]]+=1;}
queue<int> q;for(int i =0; i < indegree.size();++i){if(indegree[i]==0){
q.push(i);}}while(q.empty()==0){int cur = q.front();
res.push_back(cur);
q.pop();for(auto vertex : graph[cur]){
indegree[vertex]-=1;if(indegree[vertex]==0){
q.push(vertex);}}}for(auto i : indegree){if(i){return vector<int>();}}return res;}intmain(){int vertices, edges, front, after;
cin >> vertices >> edges;for(size_t i =1; i <= edges;++i){
cin >> front >> after;
edge[front].push_back(after);}}
强连通分量
//// Created by lanfear on 2021/2/10.//#include"iostream"#include"vector"#include"queue"#include"stack"usingnamespace std;constint MAXN =1e5;int vertices, edges, front, after;
vector<int> edge[MAXN], tEdge[MAXN];
vector<int>SCC(MAXN,0);
stack<int> Stack;bool vis[MAXN];int color;// type of sccvoidfirstDFS(constint& x){if(vis[x]){return;}
vis[x]=true;for(int i : edge[x]){firstDFS(i);}
Stack.push(x);}voidsecondDFS(constint& x){if(SCC[x]){return;}
SCC[x]= color;for(int i : tEdge[x]){secondDFS(i);}}voidkosaraju(){while(!Stack.empty()){
Stack.pop();}for(int i =1; i <= vertices;++i){firstDFS(i);}while(!Stack.empty()){if(!SCC[Stack.top()]){
color +=1;secondDFS(Stack.top());}
Stack.pop();}}intmain(){
cin >> vertices >> edges;for(int i =0; i < edges;++i){
cin >> front >> after;
edge[front].push_back(after);
tEdge[after].push_back(front);}kosaraju();for(int i =1; i <= vertices;++i){
cout <<"the"<< i <<"th vertex belongs to "<< SCC[i]<<"th SCC"<< endl;}return0;}
//// Created by lanfear on 2021/2/6.//#include"iostream"#include"vector"#include"queue"#include"stack"usingnamespace std;constint MAXN =1e5;int vertices, edges, front, after;
vector<int> edge[MAXN];
stack<int> Stack;
vector<bool>inStack(MAXN,false);
vector<int>sequence(MAXN,0);
vector<int>place(MAXN,0);
vector<vector<int>>scc(MAXN, vector<int>());int sccScale, idx =0;// maybe memorized DFS?// searching for stronglyConnectedComponents with Tarjanvoidtarjan(constint& x){
idx +=1;
sequence[x]= idx;
place[x]= idx;
Stack.push(x);
inStack[x]=true;for(int vertex : edge[x]){if(sequence[vertex]==0){tarjan(vertex);
place[x]=min(place[x], place[vertex]);}elseif(inStack[vertex]){
place[x]=min(place[x], sequence[vertex]);}}if(sequence[x]== place[x]){
sccScale +=1;int node;do{
node = Stack.top();
Stack.pop();
inStack[node]=false;
scc[sccScale -1].push_back(node);}while(node != x);}}intmain(){
cin >> vertices >> edges;for(size_t i =1; i <= edges;++i){
cin >> front >> after;
edge[front].push_back(after);}tarjan(1);
cout <<"sccScale is: "<< sccScale << endl;for(size_t i =0; i < sccScale;++i){for(int j : scc[i]){
cout << j <<'\t';}
cout << endl;}return0;}
最小生成树
//// Created by lanfear on 2021/2/16.//#include"iostream"#include"algorithm"usingnamespace std;constint MAXN =1e5+100;struct Edge {int src, dest;int weight;} edge[MAXN];int parent[MAXN];int vertices, edges;int minimumCost, cnt;boolcmp(const Edge& x,const Edge& y){return x.weight < y.weight;}intfindRoot(constint& x){return parent[x]== x ? x :findRoot(parent[x]);}voidkruskal(){for(size_t i =1; i <= vertices;++i){
parent[i]= i;}sort(edge +1, edge +1+ edges, cmp);for(size_t i =1; i <= edges;++i){int x =findRoot(edge[i].src);int y =findRoot(edge[i].dest);if(x == y){continue;}elseif(cnt == vertices -1){break;}else{
parent[y]= x;
minimumCost += edge[i].weight;
cnt +=1;}}}intmain(){
cin >> vertices >> edges;for(int i =1; i <= edges;++i){
cin >> edge[i].src >> edge[i].dest >> edge[i].weight;}kruskal();
cout << minimumCost << endl;return0;}
//// Created by lanfear on 2021/2/17.//#include"iostream"#include"algorithm"#include"cstring"usingnamespace std;constint N =1e5+100;constint INF =0x3f3f3f3f;int graph[N][N];// 邻接矩阵存图int parent[N];// 存储MSTint dis[N];// 存储外部点到MST集合的距离bool isInSet[N];// 判断点是否属于MST集合int vertices, edges;// 图顶点与边int minimumCost;// MST最小代价intminDistance(){int Min = INF, res;for(size_t i =0; i < vertices;++i){if(!isInSet[i]&& dis[i]< Min){
Min = dis[i];
res = i;}}return res;}voidprim(){memset(dis, INF,sizeof dis);for(size_t i =0; i < vertices;++i){
dis[i]= INF;
isInSet[i]=false;}// 默认首个节点作为MST树根
dis[0]=0;
parent[0]=-1;for(size_t i =0; i < vertices -1;++i){int pickedVertex =minDistance();
minimumCost += dis[pickedVertex];
isInSet[pickedVertex]=true;for(size_t j =0; j < vertices;++j){if(graph[pickedVertex][j]&&!isInSet[j]&& graph[pickedVertex][j]< dis[j]){
parent[j]= pickedVertex;
dis[j]= graph[pickedVertex][j];}}}}intmain(){int src, dest, weight;
cin >> vertices >> edges;memset(graph, INF,sizeof graph);for(int i =1; i <= edges;++i){
cin >> src >> dest >> weight;
graph[src][dest]=min(graph[src][dest], weight);
graph[dest][src]= graph[src][dest];}prim();
cout << minimumCost << endl;return0;}
最短路
//// Created by LanFear on 2021/2/17.//#include"iostream"#include"algorithm"#include"cstring"usingnamespace std;constint N =1e5+100;constint INF =0xfffffff;// 1. plain dijkstra algorithm with adjacent matrixint graph[N][N];int dist[N];bool vis[N];int vertices, edges;voiddijkstra(){memset(dist, INF,sizeof dist);
dist[1]=0;for(size_t i =1; i < vertices;++i){// find the minimum dist vertexint x =1;for(size_t j =1; j <= vertices;++j){if(!vis[j]&& dist[j]< dist[x]){
x = j;}}
vis[x]=true;// update dist infofor(size_t y =1; y <= vertices;++y){
dist[y]=min(dist[y], dist[x]+ graph[x][y]);}}}intmain(){int src, dest, weight;
cin >> vertices >> edges;// initializationmemset(graph, INF,sizeof graph);for(size_t i =1; i <= vertices;++i){
graph[i][i]=0;}for(size_t i =1; i <= edges;++i){
cin >> src >> dest >> weight;
graph[src][dest]=min(graph[src][dest], weight);}dijkstra();for(size_t i =1; i <= vertices;++i){
cout << dist[i]<< endl;}return0;}// 2. q optimized dijkstra algorithm with priority queuetypedef pair<int,int> PII;int head[N], ver[N], edge[N], Next[N];int dist[N];bool vis[N];int vertices, edges, tot;voidadd(int x,int y,int z){
ver[++tot]= y;
edge[tot]= z;
Next[tot]= head[x];
head[x]= tot;}voiddijkstra(){memset(dist,0x3f,sizeof dist);memset(vis,0,sizeof vis);
dist[1]=0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0,1});while(!q.empty()){int x = q.top().second;
q.pop();if(vis[x]){continue;}else{
vis[x]=true;for(int i = head[x]; i; i = Next[i]){int y = ver[i], z = edge[i];if(dist[y]> dist[x]+ z){
dist[y]= dist[x]+ z;
q.push({dist[y], y});}}}}}intmain(){int src, dest, weight;
cin >> vertices >> edges;// initializationfor(size_t i =1; i <= edges;++i){
cin >> src >> dest >> weight;add(src, dest, weight);}dijkstra();for(size_t i =1; i <= vertices;++i){
cout << dist[i]<< endl;}return0;}
//// Created by lanfear on 2021/2/19.//#include"iostream"#include"algorithm"#include"cstring"#include"queue"usingnamespace std;constint N =1e5+100;constint INF =0xfffffff;typedef pair<int,int> PII;int head[N], ver[N], edge[N], Next[N];
queue<int> q;int dist[N];bool vis[N];int vertices, edges, tot;voidadd(int x,int y,int z){
ver[++tot]= y;
edge[tot]= z;
Next[tot]= head[x];
head[x]= tot;}voidSPFA(){memset(dist,0x3f,sizeof dist);memset(vis,0,sizeof vis);
dist[1]=0;
q.push(1);
vis[1]=true;while(!q.empty()){int x = q.front();
q.pop();
vis[x]=false;for(int i = head[x]; i; i = Next[i]){int y = ver[i], z = edge[i];if(dist[y]> dist[x]+ z){
dist[y]= dist[x]+ z;if(!vis[y]){
q.push(y);
vis[y]=true;}}}}}intmain(){int src, dest, weight;
cin >> vertices >> edges;// initializationfor(size_t i =1; i <= edges;++i){
cin >> src >> dest >> weight;add(src, dest, weight);}SPFA();for(size_t i =1; i <= vertices;++i){
cout << dist[i]<< endl;}return0;}
//// Created by lanfear on 2021/2/19.//#include"iostream"#include"cstring"usingnamespace std;constint N =1e4+100;int graph[N][N];int vertices, edges;intmain(){
cin >> vertices >> edges;memset(graph,0x3f,sizeof graph);for(size_t i =1; i < vertices;++i){
graph[i][i]=0;}for(size_t i =1; i <= edges;++i){int src, dest, weight;
cin >> src >> dest >> weight;
graph[src][dest]=min(graph[src][dest], weight);}// floyd(dp)for(size_t k =1; k <= vertices;++k){for(size_t i =1; i <= vertices;++i){for(size_t j =1; j ,= vertices;++j){
graph[i][j]=min(graph[i][j], graph[i][k]+ graph[k][j]);}}}for(size_t i =1; i <= vertices;++i){for(size_t j =1; j <= vertices;++j){
cout << graph[i][j]<<" ";}
cout << endl;}return0;}
存图方式//// Created by lanfear on 2021/2/18.//const int N = 1e5;// 1. adjacent matrix *******************************************************************int graph[N][N];// 2. adjacency list (array) ***********************************************..