Created
November 2, 2017 20:09
-
-
Save chermehdi/9618f31775171dbf3fb4db10cc6c81d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.InputMismatchException; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.io.InputStream; | |
/** | |
* Built using CHelper plug-in | |
* Actual solution is at the top | |
* | |
* @author MaxHeap | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
InputStream inputStream = System.in; | |
OutputStream outputStream = System.out; | |
InputReader in = new InputReader(inputStream); | |
PrintWriter out = new PrintWriter(outputStream); | |
CollectingBeepers solver = new CollectingBeepers(); | |
solver.solve(1, in, out); | |
out.close(); | |
} | |
static class CollectingBeepers { | |
List<IntPair>[] g; | |
int q; | |
int[] distToStart; | |
int[][] dp; | |
int rec(int cur, int mask) { | |
if (mask == (1 << q) - 1) { | |
return distToStart[cur]; | |
} | |
if (dp[cur][mask] != -1) { | |
return dp[cur][mask]; | |
} | |
int ans = Integer.MAX_VALUE / 2; | |
for (IntPair child : g[cur]) { | |
int in = child.getFirst(); | |
if ((mask & (1 << in)) == 0) { | |
int newMask = mask | (1 << in); | |
ans = Math.min(ans, rec(in, newMask) + child.getSecond()); | |
} | |
} | |
return dp[cur][mask] = ans; | |
} | |
public void solve(int testNumber, InputReader in, PrintWriter out) { | |
int t = in.nextInt(); | |
while (t-- > 0) { | |
int n = in.nextInt(); | |
int m = in.nextInt(); | |
ArrayList<IntPair> pairs = new ArrayList<>(); | |
pairs.add(new IntPair(in.nextInt(), in.nextInt())); | |
q = in.nextInt(); | |
distToStart = new int[q + 1]; | |
for (int i = 0; i < q; i++) { | |
pairs.add(new IntPair(in.nextInt(), in.nextInt())); | |
} | |
for (int i = 1; i <= q; i++) { | |
distToStart[i] = distance(pairs.get(0), pairs.get(i)); | |
} | |
++q; | |
g = new ArrayList[q]; | |
for (int i = 0; i < q; i++) { | |
g[i] = new ArrayList<>(); | |
} | |
dp = new int[q][1 << q]; | |
for (int[] temp : dp) Arrays.fill(temp, -1); | |
for (int i = 0; i < q; i++) { | |
for (int j = i + 1; j < q; j++) { | |
int cost = distance(pairs.get(i), pairs.get(j)); | |
g[i].add(new IntPair(j, cost)); | |
g[j].add(new IntPair(i, cost)); | |
} | |
} | |
out.println(rec(0, 0)); | |
} | |
} | |
private int distance(IntPair first, IntPair second) { | |
return Math.abs(first.getFirst() - second.getFirst()) + | |
Math.abs(first.getSecond() - second.getSecond()); | |
} | |
} | |
static class IntPair implements Comparable<IntPair> { | |
int first; | |
int second; | |
public IntPair(int first, int second) { | |
this.first = first; | |
this.second = second; | |
} | |
public int compareTo(IntPair a) { | |
if (second == a.second) { | |
return Integer.compare(first, a.first); | |
} | |
return Integer.compare(second, a.second); | |
} | |
public String toString() { | |
return "<" + first + ", " + second + ">"; | |
} | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
IntPair a = (IntPair) o; | |
if (first != a.first) return false; | |
return second == a.second; | |
} | |
public int hashCode() { | |
int result = first; | |
result = 31 * result + second; | |
return result; | |
} | |
public int getFirst() { | |
return first; | |
} | |
public int getSecond() { | |
return second; | |
} | |
} | |
static class InputReader { | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public int read() { | |
if (this.numChars == -1) { | |
throw new InputMismatchException(); | |
} else { | |
if (this.curChar >= this.numChars) { | |
this.curChar = 0; | |
try { | |
this.numChars = this.stream.read(this.buf); | |
} catch (IOException ex) { | |
throw new InputMismatchException(); | |
} | |
if (this.numChars <= 0) { | |
return -1; | |
} | |
} | |
return this.buf[this.curChar++]; | |
} | |
} | |
public int nextInt() { | |
int c; | |
for (c = this.read(); isSpaceChar(c); c = this.read()) { | |
} | |
byte sgn = 1; | |
if (c == 45) { | |
sgn = -1; | |
c = this.read(); | |
} | |
int res = 0; | |
while (c >= 48 && c <= 57) { | |
res *= 10; | |
res += c - 48; | |
c = this.read(); | |
if (isSpaceChar(c)) { | |
return res * sgn; | |
} | |
} | |
throw new InputMismatchException(); | |
} | |
public static boolean isSpaceChar(int c) { | |
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment