Algorithm to improve multi-source shortest path

Resource limitation
Time limit: 3.0s Memory limit: 256.0MB
Problem description
  Given the length of the one-way edge between two pairs of n nodes, find the shortest path between them.
Input format
  The first line of input contains an integer n, representing the number of points.
  Next n lines, each line contains n integers, the i-th line represents the length of the side from the i-th point to each point, if there is no side, it is represented by 0.
Output format
  Output n lines, the i-th line represents the shortest path length from the i-th point to other points, if there is no reachable path, then output -1.
Sample input
3

0 1 0
0 0 6
0 2 0
sample output
0 1 7
-1 0 6
-1 2 0
data size and convention
  1<=n<=1000, 0<side length<=10000.

import java.util.*;

public class Main {
    
    
	static int[][]graph;
	static final int INF=0x3f3f3f3f;
	public static void floyd(int n) {
    
    
		for(int k=1;k<=n;k++) {
    
    
			for(int i=1;i<=n;i++) {
    
    
				for(int j=1;j<=n;j++) {
    
    
					if(i!=j&&graph[i][j]>graph[i][k]+graph[k][j])
						graph[i][j]=graph[i][k]+graph[k][j];
				}
			}
		}
	}
    public static void main(String[]args) {
    
    
    	Scanner sc=new Scanner(System.in);
    	int n=sc.nextInt();	
    	graph=new int[n+1][n+1];
    	for(int i=1;i<=n;i++) {
    
    
    		for(int j=1;j<=n;j++) {
    
    
    			int x=sc.nextInt();
    			if(x!=0)
    			graph[i][j]=x;
    			else graph[i][j]=INF;
    		}
    	}
    	floyd(n);
    	for(int i=1;i<=n;i++) {
    
    
    		for(int j=1;j<=n;j++) {
    
    
    			if(i!=j&&graph[i][j]!=INF)
    			System.out.print(graph[i][j]+" ");
    			else if(i!=j&&graph[i][j]==INF)
    				System.out.print(-1+" ");
    			else System.out.print(0+" ");
    		}
    		System.out.println();
    	}
    }
}

insert image description here

Referring to other people's code, it should be that reading and writing does not use IO to read and write, resulting in memory timeout

import java.io.*;
 
public class Main {
    
    
	public static void main(String[] args) throws NumberFormatException, IOException {
    
    
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int INF = 65535;
		int [][]dis = new int[n][n];
		for(int i = 0; i < n;i++) {
    
    
			String[] split = br.readLine().split(" ");
			for(int j = 0; j < n;j++) {
    
    
				if(Integer.parseInt(split[j]) == 0) {
    
    
					dis[i][j] = INF;
				}else {
    
    
					dis[i][j] = Integer.parseInt(split[j]);
				}
			}
		}
		Graph1057 graph = new Graph1057(dis);
		graph.floyd();
		graph.print();
	}
	
}
class Graph1057{
    
    
	int n;
	int [][]dis;
	int INF = 65535;
 
	public Graph1057(int[][] dis) {
    
    
		this.dis = dis;
		n = dis.length;
	}
	
	public void floyd() {
    
    
		for(int k = 0; k < n;k++) {
    
    
			for(int i = 0; i < n;i++) {
    
    
				for(int j = 0; j < n;j++) {
    
    
					int len = dis[i][k] + dis[k][j];
					if(len < dis[i][j]) {
    
    
						dis[i][j] = len;
					}
				}
			}
		}
	}
	
	public void print() {
    
    
		for(int i = 0; i < n;i++) {
    
    
			for(int j = 0; j < n;j++) {
    
    
				if(i == j) {
    
    
					System.out.print("0 ");
				}else if(dis[i][j] == INF){
    
    
					System.out.print("-1 ");
				}else {
    
    
					System.out.print(dis[i][j] + " ");
				}
			}
			System.out.println();
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_54537215/article/details/123869588