AcWing 854. Floyd求最短路-java
原题链接
简单
作者:
Susu
,
2020-01-30 15:24:35
,
所有人可见
,
阅读 714
import java.util.*;
public class Main{
static int N = 510;
static int n,m,k;
static int[][] dist = new int[N][N];
static void floyd(){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=Math.min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(i==j) dist[i][j]=0;
else dist[i][j]=0x3f3f3f;
}
}
while(m-->0){
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
dist[x][y] = Math.min(dist[x][y], z);
}
floyd();
while(k-->0){
int x = sc.nextInt();
int y = sc.nextInt();
if(dist[x][y]>0x3f3f3f/2) System.out.println("impossible");
else System.out.println(dist[x][y]);
}
}
}