forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1724 ROADS.java
71 lines (70 loc) · 2.1 KB
/
1724 ROADS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
class Road {
int dest;
int length;
int cost;
Road(int dest, int length, int cost) {
this.dest = dest;
this.length = length;
this.cost = cost;
}
}
public class Main {
private static final int MAX = 0x3f3f3f3f;
private static int rst = MAX;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int totalMoney = in.nextInt();
int cityNumber = in.nextInt();
int roadNumber = in.nextInt();
boolean[] visited = new boolean[cityNumber + 1];
int[][] minLength = new int[cityNumber + 1][totalMoney + 1];
List<List<Road>> map = new ArrayList<List<Road>>();
for (int i = 0; i <= cityNumber; ++i) {
map.add(new ArrayList<Road>());
}
for (int i = 0; i < roadNumber; ++i) {
int start = in.nextInt();
Road r = new Road(in.nextInt(), in.nextInt(), in.nextInt());
if (r.dest != start) {
map.get(start).add(r);
}
}
for (int i = 0; i <= cityNumber; ++i) {
for (int j = 0; j <= totalMoney; ++j) {
minLength[i][j] = MAX;
}
}
visited[1] = true;
helper(map, visited, minLength, 0, 0, 1, totalMoney, cityNumber);
rst = (rst == MAX) ? -1 : rst;
System.out.println(rst);
}
private static void helper(List<List<Road>> map, boolean[] visited, int[][] minLength, int curLen, int curCost, int curLoc, int totalMoney, int dest) {
if (curLoc == dest) {
rst = Math.min(curLen, rst);
return;
}
for (int i = 0; i < map.get(curLoc).size(); ++i) {
int nextLoc = map.get(curLoc).get(i).dest;
if (!visited[nextLoc]) {
int cost = curCost + map.get(curLoc).get(i).cost;
if (cost > totalMoney) {
continue;
}
if (curLen + map.get(curLoc).get(i).length >= rst ||
curLen + map.get(curLoc).get(i).length >= minLength[nextLoc][cost]) {
continue;
}
curLen += map.get(curLoc).get(i).length;
curCost += map.get(curLoc).get(i).cost;
minLength[nextLoc][cost] = curLen;
visited[nextLoc] = true;
helper(map, visited, minLength, curLen, curCost, nextLoc, totalMoney, dest);
visited[nextLoc] = false;
curLen -= map.get(curLoc).get(i).length;
curCost -= map.get(curLoc).get(i).cost;
}
}
}
}