View Code of Problem 2596

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader brd = new BufferedReader(new InputStreamReader(System.in));
		String string;
		while((string = brd.readLine() )!= null) {
			String[] nmt = string.split(" ");
			int n =Integer.parseInt(nmt[0]);
			int m =Integer.parseInt(nmt[1]);
			int t =Integer.parseInt(nmt[2]);
			String[] se = brd.readLine().split(" ");
			int s =Integer.parseInt(se[0])-1;
			int e =Integer.parseInt(se[1])-1;
			int[][] way = new int[n][n];
			int[] place = new int[n];
			for(int i =0;i<m;i++) {
				String[] abc = brd.readLine().split(" ");
				int a =Integer.parseInt(abc[0])-1;
				int b =Integer.parseInt(abc[1])-1;
				int c =Integer.parseInt(abc[2]);
				way[a][b]=c;
				way[b][a]=c;
			}
			int cost = findm(way,place,s,e,t,0);
			if(cost==-1) {
				System.out.println("I am sorry,jlh!");
			}else {
				System.out.println(cost);
			}
		}
	}
	public static int findm(int[][] way,int[] place ,int s,int e,int t,int cost) {
		place[s]=1;
		if(cost>t) {
			return -1;
		}else {
			if(s==e) {
				return cost;
			}else {
				int min=Integer.MAX_VALUE;
				for(int i =0;i<way[s].length;i++) {
					if(place[i]==0 && way[s][i]!=0) {
						place[i]=1;
						int c = findm(way,place,i,e,t,cost+10*way[s][i]);
						place[i]=0;
						if(min>c) {
							min=c;
						}
					}
				}
				return min;
			}
		}
	}
}

Double click to view unformatted code.


Back to problem 2596