import java.util.*; public class Main { public static void main(String[] args) { Scanner in =new Scanner(System.in); int n =in.nextInt(); int m =in.nextInt(); int k =in.nextInt(); int[][] road = new int[m][m]; for(int i =0;i<m;i++) { int a=in.nextInt()-1; int b=in.nextInt()-1; int l=in.nextInt(); road[a][b]=l; road[b][a]=l; } for(int i =0;i<k;i++) { int start = in.nextInt()-1; int end = in.nextInt()-1; System.out.println(findway(road,end,start,0)); } } public static int findway(int[][] road,int end,int loc,int dis) { if(loc==end) { return dis; }else { int min = Integer.MAX_VALUE; for(int i =0;i<road[loc].length;i++) { if(road[loc][i]==0) { continue; }else { int r = road[loc][i]; road[loc][i]=0; road[i][loc]=0; int d = findway(road,end,i,dis+r); road[loc][i]=r; road[i][loc]=r; if(d<min) { min=d; } } } return min; } } } |
Double click to view unformatted code.