2024蓝桥杯赛前模版突击:图论篇
图论在蓝桥杯中一般考的不难,如果有图论的题,就基本是模板题,知道板子就有分了。
邻接表
本文使用方法1的方式实现邻接表
邻接表1
static int[] dist = new int[N],st = new int[N];
static int[] h = new int[N],e = new int[M],ne = new int[M],w = new int[M];
static int idx;
static void init(){
Arrays.fill(h,-1);
}
static void add(int a,int b,int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
邻接表2
用来快速得到顶点的所有邻边条数
leetcode中比较常见
ArrayList<Integer>[] g = new ArrayList[N];
//初始化
for(int i=0;i<n;i++)
g[i] = new ArrayList<Integer>();
//顶点a,b中间添加一条边
g[a].add(b);
最短路Dijkstra
单源最短路 O(mlogn)
package _00模板;
import java.util.*;
public class Dijkstra {
static int INF = 0x3f3f3f3f;
static int N = 101000,M = 2*N;
static int[] dist = new