-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.java
80 lines (65 loc) · 2.06 KB
/
route.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
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class route {
static int n, m, r;
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("route.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("route.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
int[] leftVal = new int[n];
for (int i = 0; i < n; i++) {
int val = Integer.parseInt(f.readLine());
leftVal[i] = val;
}
int[] rightVal = new int[m];
for (int i = 0; i < m; i++) {
int val = Integer.parseInt(f.readLine());
rightVal[i] = val;
}
Edge5[] edges = new Edge5[r];
for (int i = 0; i < r; i++) {
st = new StringTokenizer(f.readLine());
int left = Integer.parseInt(st.nextToken()) - 1;
int right = Integer.parseInt(st.nextToken()) - 1;
edges[i] = new Edge5(left, right);
}
Arrays.sort(edges);
int[] leftdp = new int[n];
int[] rightdp = new int[m];
for (int i = 0; i < n; i++)
leftdp[i] = leftVal[i];
for (int i = 0; i < m; i++)
rightdp[i] = rightVal[i];
for (int i = 0; i < r; i++) {
Edge5 e = edges[i];
int leftOrig = leftdp[e.left];
int rightOrig = rightdp[e.right];
leftdp[e.left] = Math.max(leftdp[e.left], rightOrig + leftVal[e.left]);
rightdp[e.right] = Math.max(rightdp[e.right], leftOrig + rightVal[e.right]);
}
int ans = 0;
for (int i = 0; i < n; i++)
ans = Math.max(ans, leftdp[i]);
for (int i = 0; i < m; i++)
ans = Math.max(ans, rightdp[i]);
out.println(ans);
out.close();
}
}
class Edge5 implements Comparable<Edge5> {
int left, right;
public Edge5(int left, int right) {
this.left = left;
this.right = right;
}
@Override
public int compareTo(Edge5 other) {
if (this.left == other.left)
return this.right - other.right;
return this.left - other.left;
}
}