View Code of Problem 2596

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		while(in.hasNext()) {
			int n =in.nextInt();
			int m =in.nextInt();
			int t =in.nextInt();
			int s =in.nextInt()-1;
			int e =in.nextInt()-1;
			int[][] way = new int[n][n];
			int[] place = new int[n];
			for(int i =0;i<m;i++) {
				int a =in.nextInt()-1;
				int b =in.nextInt()-1;
				int c =in.nextInt();
				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