前言
今天主要复习了最短路中的dijkstra算法、最近公共祖先(LCA)、最小生成树、拓扑排序这四个部分的知识。
一、最短路
题目背景
主要代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<vector>
#include<queue>
#define MAXN 100000+10
#define MAXM 1000000+10
#define INF 0x7fffffff
using namespace std;
struct node
{
int to,next,w;
}e[MAXM];
int n,m,s=1,cnt=0,u,v,w;
int head[MAXM],dist[MAXN],vis[MAXN];
int read()
{
int x=0;
char c=getchar();
while(c<'0'||c>'9')
{
c=getchar();
}
while(c>='0'&& c<='9')
{
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
}
return x;
}
void add(int u,int v,int w)//前向星建图
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt++;
return ;
}
struct cmp//定义规则
{
bool operator()(int a,int b)
{
return dist[a]>dist[b];
}
};
void solve()//用迪杰斯特拉算法实现
{
dist[s]=0;
vis[s]=0;
priority_queue<int,vector<int>,cmp> q;
q.push(s);
while(!q.empty())
{
int t=q.top();
q.pop();
vis[t]=false;
for(int i=head[t];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(dist[v]>dist[t]+e[i].w)
{
dist[v]=dist[t]+e[i].w;
if(!vis[v])
{
q.push(v);
vis[v