forked from liuchuo/PAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1030 Travel Plan (30).java
103 lines (92 loc) · 3.31 KB
/
1030 Travel Plan (30).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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static class Info {
int distance, cost;
Info(int distance, int cost) {
this.distance = distance;
this.cost = cost;
}
}
static class Graph {
private final int vertex, edges, end, start;
private final Info[][] tables;
Graph() throws IOException {
String[] split = reader.readLine().split(" ");
this.vertex = Integer.valueOf(split[0]);
this.edges = Integer.valueOf(split[1]);
this.start = Integer.valueOf(split[2]);
this.end = Integer.valueOf(split[3]);
tables = new Info[vertex][vertex];
for (int i = 0; i < edges; i++) {
String[] lineInfo = reader.readLine().split(" ");
int s = Integer.valueOf(lineInfo[0]);
int e = Integer.valueOf(lineInfo[1]);
int distance = Integer.valueOf(lineInfo[2]);
int cost = Integer.valueOf(lineInfo[3]);
tables[s][e] = new Info(distance, cost);
tables[e][s] = new Info(distance, cost);
}
}
Node bfs() {
Node node = new Node(start);
boolean[] checkTable = new boolean[vertex];
PriorityQueue<Node> queue = new PriorityQueue<>();
queue.add(node);
while (!queue.isEmpty()) {
Node poll = queue.poll();
if (poll.id == end) {
return poll;
}
checkTable[poll.id] = true;
for (int i = 0; i < vertex; i++) {
Info info = tables[poll.id][i];
if (info != null && !checkTable[i]) {
Node newNode = new Node(i);
newNode.previous = poll;
newNode.distance = poll.distance + info.distance;
newNode.cost = poll.cost + info.cost;
queue.offer(newNode);
}
}
}
return null;
}
}
static class Node implements Comparable<Node> {
int id, distance, cost;
Node previous;
Node(int id) {
this.id = id;
}
@Override
public int compareTo(Node o) {
int compare = distance - o.distance;
if (compare == 0) {
compare = id - o.id;
}
return compare == 0 ? cost - o.cost : compare;
}
}
public static void main(String[] args) throws IOException {
Graph graph = new Graph();
Node bfs = graph.bfs();
LinkedList<String> list = new LinkedList<>();
list.addFirst(String.valueOf(bfs.cost));
list.addFirst(String.valueOf(bfs.distance));
while (bfs != null) {
list.addFirst(String.valueOf(bfs.id));
bfs = bfs.previous;
}
int last = list.size() - 1;
int start = 0;
for (String o : list) {
System.out.printf("%s%s", o, start == last ? "\n" : " ");
start++;
}
}
}