import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; class Node { int i, j; public Node(int i, int j) { this.i = i; this.j = j; } @Override public String toString() { return i + "-" + j; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + i; result = prime * result + j; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Node other = (Node) obj; if (i != other.i) return false; if (j != other.j) return false; return true; } } public class kug { private static final String TEST_FILE = "kug1.in"; private PrintWriter pw = null; private BufferedReader br = null; private static final Node NODE0 = new Node(0,0); private static final List<Node> PATH0 = new ArrayList<Node>(); static { PATH0.add(NODE0); } int N; int table[][]; kug(boolean skip) throws FileNotFoundException { this.pw = new PrintWriter(System.out); this.br = new BufferedReader(new InputStreamReader(System.in), 1 << 16); if (skip) return; File file = new File(TEST_FILE); if (file.exists()) { br = new BufferedReader(new java.io.FileReader(TEST_FILE)); } else { br = new BufferedReader(new InputStreamReader(System.in), 1 << 16); } } void doit() throws IllegalArgumentException, IOException { N = Integer.parseInt(br.readLine()); table = new int[N + 1][]; //table[0] = new int[] { 0 }; for (int i = 1; i <= N; i++) { table[i] = new int[N + 1]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int j = 1; j <= N + 1 - i; j++) { table[i][j] = Integer.parseInt(st.nextToken()); } //System.out.println(Arrays.toString(table[i])); } List<List<Node>> koszty = new ArrayList<List<Node>>(); koszty.add(new ArrayList<Node>()); koszty.get(0).add(NODE0); Map<Node, Integer> nodes2costs = new HashMap<Node, Integer>(); nodes2costs.put(NODE0, 0); for (int ii = 1; ii <= N; ii++) { List<Node> l = new ArrayList<Node>(); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (ii >= i && ii <= j) { Node key = new Node(i, j); l.add(key); int koszt = table[ii][j - ii + 1]; //System.out.println(ii + ", " + j + " koszt: " + koszt); key = new Node(ii, j); nodes2costs.put(key, koszt); } } } koszty.add(l); } koszty.add(PATH0); //System.out.println(nodes2costs); // for (int i = 0; i <= N + 1; i++) { // List<Node> l = koszty.get(i); // Node[] s = l.toArray(new Node[] {}); // System.out.println(i + ": " + Arrays.toString(s)); // } Map<List<Node>, Integer> pathsToCosts = new HashMap<List<Node>, Integer>(); pathsToCosts.put(PATH0 , 0); for (int i = 1; i <= N; i++) { List<Node> l = koszty.get(i); Map<List<Node>, Integer> newPathsToCosts = new HashMap<List<Node>, Integer>(); for (Node node : l) { internal: for (List<Node> nd : pathsToCosts.keySet()) { // if (nd.size() != i) { // continue internal; // } if (nd.contains(node)) { continue internal; } Integer cost = pathsToCosts.get(nd); if (cost != null) { //int koszt = getCost(node); int koszt = nodes2costs.get(node); newPathsToCosts.put(getPath(nd, node), koszt + cost); } } } //pathsToCosts.putAll(newPathsToCosts); pathsToCosts = newPathsToCosts; } int min = Integer.MAX_VALUE; for (List<Node> nd : pathsToCosts.keySet()) { //System.out.println(nd + " " + pathsToCosts.get(nd)); // if (nd.size() != N + 1) { // continue; // } if (pathsToCosts.get(nd) < min) { min = pathsToCosts.get(nd); } } pw.println(min); // for (List<Node> nd : pathsToCosts.keySet()) { // if (nd.size() != N + 1) { // continue; // } // if (min == pathsToCosts.get(nd)) { // System.out.println(nd + " " + pathsToCosts.get(nd)); // } // } pw.flush(); } public static void main(String[] args) throws IllegalArgumentException, IOException { new kug(true).doit(); } // private int getCost(Node node) { // return table[node.i][N - node.j + 1]; // } // // private int getCost(List<Node> nodes) { // int sum = 0; // for (Node nd : nodes) { // sum += table[nd.i][nd.j]; // } // return sum; // } // // private List<Node> getPath(Node nd) { // List<Node> path = new ArrayList<Node>(); // path.add(nd); // return path; // } // // private List<Node> getPath(Node...nodes) { // List<Node> path = new ArrayList<Node>(); // for (Node nd : nodes) { // path.add(nd); // } // return path; // } private List<Node> getPath(List<Node> nodes, Node nd) { List<Node> path = new ArrayList<Node>(); path.addAll(nodes); path.add(nd); return path; } }
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; class Node { int i, j; public Node(int i, int j) { this.i = i; this.j = j; } @Override public String toString() { return i + "-" + j; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + i; result = prime * result + j; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Node other = (Node) obj; if (i != other.i) return false; if (j != other.j) return false; return true; } } public class kug { private static final String TEST_FILE = "kug1.in"; private PrintWriter pw = null; private BufferedReader br = null; private static final Node NODE0 = new Node(0,0); private static final List<Node> PATH0 = new ArrayList<Node>(); static { PATH0.add(NODE0); } int N; int table[][]; kug(boolean skip) throws FileNotFoundException { this.pw = new PrintWriter(System.out); this.br = new BufferedReader(new InputStreamReader(System.in), 1 << 16); if (skip) return; File file = new File(TEST_FILE); if (file.exists()) { br = new BufferedReader(new java.io.FileReader(TEST_FILE)); } else { br = new BufferedReader(new InputStreamReader(System.in), 1 << 16); } } void doit() throws IllegalArgumentException, IOException { N = Integer.parseInt(br.readLine()); table = new int[N + 1][]; //table[0] = new int[] { 0 }; for (int i = 1; i <= N; i++) { table[i] = new int[N + 1]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int j = 1; j <= N + 1 - i; j++) { table[i][j] = Integer.parseInt(st.nextToken()); } //System.out.println(Arrays.toString(table[i])); } List<List<Node>> koszty = new ArrayList<List<Node>>(); koszty.add(new ArrayList<Node>()); koszty.get(0).add(NODE0); Map<Node, Integer> nodes2costs = new HashMap<Node, Integer>(); nodes2costs.put(NODE0, 0); for (int ii = 1; ii <= N; ii++) { List<Node> l = new ArrayList<Node>(); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (ii >= i && ii <= j) { Node key = new Node(i, j); l.add(key); int koszt = table[ii][j - ii + 1]; //System.out.println(ii + ", " + j + " koszt: " + koszt); key = new Node(ii, j); nodes2costs.put(key, koszt); } } } koszty.add(l); } koszty.add(PATH0); //System.out.println(nodes2costs); // for (int i = 0; i <= N + 1; i++) { // List<Node> l = koszty.get(i); // Node[] s = l.toArray(new Node[] {}); // System.out.println(i + ": " + Arrays.toString(s)); // } Map<List<Node>, Integer> pathsToCosts = new HashMap<List<Node>, Integer>(); pathsToCosts.put(PATH0 , 0); for (int i = 1; i <= N; i++) { List<Node> l = koszty.get(i); Map<List<Node>, Integer> newPathsToCosts = new HashMap<List<Node>, Integer>(); for (Node node : l) { internal: for (List<Node> nd : pathsToCosts.keySet()) { // if (nd.size() != i) { // continue internal; // } if (nd.contains(node)) { continue internal; } Integer cost = pathsToCosts.get(nd); if (cost != null) { //int koszt = getCost(node); int koszt = nodes2costs.get(node); newPathsToCosts.put(getPath(nd, node), koszt + cost); } } } //pathsToCosts.putAll(newPathsToCosts); pathsToCosts = newPathsToCosts; } int min = Integer.MAX_VALUE; for (List<Node> nd : pathsToCosts.keySet()) { //System.out.println(nd + " " + pathsToCosts.get(nd)); // if (nd.size() != N + 1) { // continue; // } if (pathsToCosts.get(nd) < min) { min = pathsToCosts.get(nd); } } pw.println(min); // for (List<Node> nd : pathsToCosts.keySet()) { // if (nd.size() != N + 1) { // continue; // } // if (min == pathsToCosts.get(nd)) { // System.out.println(nd + " " + pathsToCosts.get(nd)); // } // } pw.flush(); } public static void main(String[] args) throws IllegalArgumentException, IOException { new kug(true).doit(); } // private int getCost(Node node) { // return table[node.i][N - node.j + 1]; // } // // private int getCost(List<Node> nodes) { // int sum = 0; // for (Node nd : nodes) { // sum += table[nd.i][nd.j]; // } // return sum; // } // // private List<Node> getPath(Node nd) { // List<Node> path = new ArrayList<Node>(); // path.add(nd); // return path; // } // // private List<Node> getPath(Node...nodes) { // List<Node> path = new ArrayList<Node>(); // for (Node nd : nodes) { // path.add(nd); // } // return path; // } private List<Node> getPath(List<Node> nodes, Node nd) { List<Node> path = new ArrayList<Node>(); path.addAll(nodes); path.add(nd); return path; } } |