Bellman_ford 队列优化算法(又名SPFA)
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
//初始化
List<List<int[]>> edges = new ArrayList<>();
for(int i=0;i<=n;i++) {
List<int[]> temp = new ArrayList<>();
edges.add(temp);
}
int[][] graph = new int[n+1][n+1];
for(int i=0;i<=n;i++) {
Arrays.fill(graph[i], Integer.MAX_VALUE);
}
int[] minDp = new int[n+1];
Arrays.fill(minDp, Integer.MAX_VALUE);
boolean[] visited = new boolean[n+1]; //标记是否放入队列中
//接收数据
for(int i=0;i<m;i++) {
int x = scan.nextInt();
int y = scan.nextInt();
int val = scan.nextInt();
graph[x][y]=val;
int[] temp = new int[] {x,y,val};
edges.get(x).add(temp);
}
//松弛操作
Queue<Integer> queue = new LinkedList<>();
queue.add(1);
minDp[1]=0;
while(!queue.isEmpty()) {
int cur = queue.remove();
visited[cur]=false;
List<int[]> temp = edges.get(cur);
for(int i=0;i<temp.size();i++) {
int[] edge = temp.get(i);
if(minDp[edge[0]]!=Integer.MAX_VALUE&&minDp[edge[1]]>minDp[edge[0]]+edge[2]) {
minDp[edge[1]]=minDp[edge[0]]+edge[2];
if(visited[edge[1]]==false) {
queue.add(edge[1]);
visited[edge[1]]=true;
}
}
}
}
//输出
if(minDp[n]==Integer.MAX_VALUE) System.out.println("unconnected");
else System.out.println(minDp[n]);
scan.close();
}
Bellman_ford之判断负权回路
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
//初始化
int[][] edges = new int[m][3];
int[][] graph = new int[n+1][n+1];
for(int i=0;i<=n;i++) {
Arrays.fill(graph[i], Integer.MAX_VALUE);
}
int[] minDp = new int[n+1];
Arrays.fill(minDp, Integer.MAX_VALUE);
//接收数据
for(int i=0;i<m;i++) {
int x = scan.nextInt();
int y = scan.nextInt();
int val = scan.nextInt();
graph[x][y]=val;
edges[i][0]=x;
edges[i][1]=y;
edges[i][2]=val;
}
//松弛操作
minDp[1]=0;
boolean flag=false;
for(int i=0;i<=n;i++) {
for(int j=0;j<m;j++) {
int[] edge = edges[j];
if(minDp[edge[0]]!=Integer.MAX_VALUE&&minDp[edge[1]]>minDp[edge[0]]+edge[2]) {
minDp[edge[1]]=minDp[edge[0]]+edge[2];
if(i==n)flag=true;
}
}
}
//输出
if(flag==false) {
if(minDp[n]==Integer.MAX_VALUE) System.out.println("unconnected");
else System.out.println(minDp[n]);
}else {
System.out.println("circle");
}
scan.close();
}
Bellman_ford之单源有限最短路
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
//初始化
int[][] edges = new int[m][3];
int[][] graph = new int[n+1][n+1];
for(int i=0;i<=n;i++) {
Arrays.fill(graph[i], Integer.MAX_VALUE);
}
int[] minDp = new int[n+1];
Arrays.fill(minDp, Integer.MAX_VALUE);
//接收数据
for(int i=0;i<m;i++) {
int x = scan.nextInt();
int y = scan.nextInt();
int val = scan.nextInt();
graph[x][y]=val;
edges[i][0]=x;
edges[i][1]=y;
edges[i][2]=val;
}
int start = scan.nextInt();
int end = scan.nextInt();
int key = scan.nextInt();
//松弛操作(key+1次,因为经过一个站点有两条边)
minDp[start]=0;
for(int i=0;i<=key;i++) {
int[] minCopy = Arrays.copyOf(minDp, n + 1);
for(int j=0;j<m;j++) {
int[] edge = edges[j];
if(minCopy[edge[0]]!=Integer.MAX_VALUE&&minDp[edge[1]]>minCopy[edge[0]]+edge[2]) {
minDp[edge[1]]=minCopy[edge[0]]+edge[2];
}
}
}
//输出
if(minDp[end]==Integer.MAX_VALUE) System.out.println("unreachable");
else System.out.println(minDp[end]);
scan.close();
}