|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +class Point { |
| 51 | + long x, y; |
| 52 | + Point(long x, long y) { |
| 53 | + this.x = x; |
| 54 | + this.y = y; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +public class Main { |
| 59 | + |
| 60 | + static IOController io; |
| 61 | + |
| 62 | + // |
| 63 | + |
| 64 | + static final int INF = (int)1e9 + 7; |
| 65 | + static final int[] dx = {0,1,0,-1}; |
| 66 | + static final int[] dy = {1,0,-1,0}; |
| 67 | + |
| 68 | + static int N, K, R; |
| 69 | + static int[][][] cost; |
| 70 | + static int[][] points; |
| 71 | + |
| 72 | + public static void main(String[] args) throws Exception { |
| 73 | + |
| 74 | + io = new IOController(); |
| 75 | + |
| 76 | + init(); |
| 77 | + solve(); |
| 78 | + |
| 79 | + io.close(); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + static void init() throws Exception { |
| 84 | + |
| 85 | + N = io.nextInt(); |
| 86 | + K = io.nextInt(); |
| 87 | + R = io.nextInt(); |
| 88 | + cost = new int[N+1][N+1][4]; |
| 89 | + for(int i=0;i<R;i++) { |
| 90 | + int a = io.nextInt(); |
| 91 | + int b = io.nextInt(); |
| 92 | + int c = io.nextInt(); |
| 93 | + int d = io.nextInt(); |
| 94 | + if(a == c) { |
| 95 | + if(b+1 == d) { |
| 96 | + cost[a][b][0] = cost[c][d][2] = 1; |
| 97 | + } |
| 98 | + else { |
| 99 | + cost[a][b][2] = cost[c][d][0] = 1; |
| 100 | + } |
| 101 | + } |
| 102 | + else { |
| 103 | + if(a+1 == c) { |
| 104 | + cost[a][b][1] = cost[c][d][3] = 1; |
| 105 | + } |
| 106 | + else { |
| 107 | + cost[a][b][3] = cost[c][d][1] = 1; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + points = new int[K][2]; |
| 112 | + for(int i=0;i<K;i++) points[i] = new int[]{io.nextInt(), io.nextInt()}; |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + static void solve() throws Exception { |
| 117 | + |
| 118 | + int ans = 0; |
| 119 | + for(int[] point : points) { |
| 120 | + int x = point[0], y = point[1]; |
| 121 | + int[][] dist = dijk(x, y); |
| 122 | + for(int i=0;i<K;i++) { |
| 123 | + int xx = points[i][0], yy = points[i][1]; |
| 124 | + if(dist[xx][yy] > 0) ans++; |
| 125 | + } |
| 126 | + } |
| 127 | + io.write((ans/2) + "\n"); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + static int[][] dijk(int sx, int sy) { |
| 132 | + Deque<int[]> deque = new ArrayDeque<>(); |
| 133 | + int[][] dist = new int[N+1][N+1]; |
| 134 | + for(int i=1;i<=N;i++) Arrays.fill(dist[i], INF); |
| 135 | + deque.offer(new int[]{0, sx, sy}); |
| 136 | + dist[sx][sy] = 0; |
| 137 | + while(!deque.isEmpty()) { |
| 138 | + int[] cur = deque.pollFirst(); |
| 139 | + int d = cur[0], x = cur[1], y = cur[2]; |
| 140 | + for(int i=0;i<4;i++) { |
| 141 | + int xx = x+dx[i], yy = y+dy[i]; |
| 142 | + if(xx<1 || xx>N || yy<1 || yy>N) continue; |
| 143 | + int nextCost = d + cost[x][y][i]; |
| 144 | + if(dist[xx][yy] > nextCost) { |
| 145 | + if(d == nextCost) { |
| 146 | + deque.addFirst(new int[]{nextCost, xx, yy}); |
| 147 | + } |
| 148 | + else { |
| 149 | + deque.addLast(new int[]{nextCost, xx, yy}); |
| 150 | + } |
| 151 | + dist[xx][yy] = nextCost; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + return dist; |
| 156 | + } |
| 157 | + |
| 158 | +} |
| 159 | +``` |
0 commit comments